我正在尝试像这样将文件解码为位图 -
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
的帮助下从另一个 Activity 中读取图像最佳答案
在您的 createImageFile() 方法中创建/保存文件路径:mCurrentPhotoPath
。
File imageFile = new File(image.getAbsolutePath());
mCurrentPhotoPath = imageFile.toString();
关于android - 为什么 Bitmap.decodeFile() 方法返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787483/