【发布时间】:2016-08-05 10:53:02
【问题描述】:
我正在尝试像这样将文件解码为位图 -
BitmapFactory.Options options = new BitmapFactory.Options();
try {
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
cropImageView.setImageBitmap(bitmap);
Log.d("Musik","bitmap - "+bitmap+" "+mCurrentPhotoPath);
}
catch (Exception e){
Log.d("Musik",e.toString());
}
我的 mCurrentPhotoPath 是
file:/storage/sdcard0/Android/data/com.example.iaugmentor.iaugmentortestvers/files/Pictures/JPEG_20160805_161105_478260200.jpg
我无法找到它返回 null 的原因,也没有捕获到异常。
请帮忙。提前致谢。
为了清楚起见,让我先说明我是如何保存图像的,我首先创建一个 File 对象,然后捕获图像并将其保存在给定的 Uri 中 -
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (items[which].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null) {
// Continue only if the File was successfully created
if (photoFile != null) {
profilePicUri = FileProvider.getUriForFile(ApplicationContext.getAppContext(),
"com.example.iaugmentor.iaugmentortestvers",
photoFile);
}
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, profilePicUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, RESULT_LOAD_IMG);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
createImageFile() 方法是这样的`
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
然后我试图在变量 mCurrentPhotoPath
的帮助下从另一个活动中读取图像【问题讨论】:
-
在清单文件中添加了您的读取权限?此外,如果您的 API 级别为 23,那么您应该添加运行时权限。
-
@PiyushGupta 是的,我添加了读取权限顺便说一句,运行时权限是什么意思?
-
如果您的目标版本是 23,那么您应该为读取外部存储权限添加运行时权限。检查this了解更多信息
-
@PiyushGupta 我的目标版本实际上是 24,我确实像你说的那样,但它仍然返回 null
标签: android bitmap bitmapfactory