Android Bitmapfactory.decodeStream引发FileNotFoundException
我正在尝试从内部存储器保存和检索位图,但每次尝试加载位图时,BitMapFactory都会引发异常:Android Bitmapfactory.decodeStream引发FileNotFoundException,android,Android,我正在尝试从内部存储器保存和检索位图,但每次尝试加载位图时,BitMapFactory都会引发异常: BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: android.graphics.Bitmap@b35e414: open failed: ENOENT (No such file or directory) 我在这个网站上尝试了几乎所有类似线程给出的解决方案,但没有一个对我有效。 这个异常被抛出了4
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: android.graphics.Bitmap@b35e414: open failed: ENOENT (No such file or directory)
我在这个网站上尝试了几乎所有类似线程给出的解决方案,但没有一个对我有效。
这个异常被抛出了4次,尽管我只读取了一幅图像。如何处理
这是我用来从存储器中保存和检索图像的代码
public static void saveFile(Context context, Bitmap b, String picName) {
FileOutputStream fos;
try {
fos = context.openFileOutput(picName, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (IOException e) {
Log.e("store DRV image", e.getMessage());
e.printStackTrace();
}
}
public static Bitmap loadBitmap(Context context, String picName) {
Bitmap b = null;
FileInputStream fis;
try {
fis = context.openFileInput(picName);
b = BitmapFactory.decodeStream(fis);
fis.close();
} catch (IOException e) {
Log.e("get stored DRV image", e.getMessage());
e.printStackTrace();
}
return b;
}
我从这个网站的一个帖子里得到了这个代码,所有的评论都很好。但这对我不起作用。我在清单中添加了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
最后,我使用随机生成的UID作为文件名。UID是使用Firebase SDK生成的。因此UID可能包含数字或其他字符,如
xxgkbris5ogqz1euqiyrsc1gbs2
。那么这是一种错误的文件命名方式吗?因此抛出异常 您需要在6.0以上添加用户权限:
Add library:
compile 'pub.devrel:easypermissions:0.2.1'
private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
pickImageFromGallery();
} else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}
在活动的onCreate()中添加以下代码:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
使用以下命令捕获同一活动中的结果:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
//call your method
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
了解有关运行时权限的详细信息,请访问//确保已授予权限 //为了节省
File file=saveFile(contex,bitmap,picName);
//For fetching
File dir = new File(Environment.getExternalStorageDirectory(), picName);
BitmapFactory.decodeFile(file.getPath())
/**
* @param context
* @param b
* @param picName
*/
public static File saveFile(Context context, Bitmap b, String picName) {
FileOutputStream fos;
File dir = new File(Environment.getExternalStorageDirectory(), "My Images");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, picName);
try {
fos = context.openFileOutput(file.getPath(), Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (IOException e) {
Log.e("store DRV image", e.getMessage());
e.printStackTrace();
}
return file;
}
请共享picName值或其所在的代码initialize@AnujJPandeyPicname是
xxgkbris5ogqz1euqiyrsc1gbs2
,我甚至尝试使用myImage
和myImage.png
。但引发了异常。您正在使用哪个android操作系统?@JdPrajapati我正在android 6.01 Marshmallow上测试。您是否添加了运行时权限?运行时权限对话框未打开。我已经按照你的建议在onCreate中请求了权限。你在gradle中的目标sdk是什么?targetSdkVersion是23清除你的应用程序数据并清除安装apk并运行代码…对话框应该显示异常被抛出saveFile
或loadBitmap
?我将尝试使用它。因此,在获取时,不需要在文件路径中添加“MyImages”?在保存获取时使用此代码IllegalArgumentException包含路径分隔符
错误。