从图库中选择图像后,BitmapFactory.decode()生成文件未找到错误

时间:2016-03-14 15:44:22

标签: android android-camera android-bitmap

解码从 action_pick 意图中的 Media.EXTRA_OUTPUT 收到的图像文件,生成文件未找到异常。我看到很多类似的问题,但仍然无法弄清楚问题。我在 Lollipop 下方的Android 图库应用中遇到此问题,并且 Google照片应用上方的问题与Android版&gt; = < / strong>即可。

Myfragment.java

if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Helpers.getOutputMediaFileUri());

    Intent startImage = Intent.createChooser(chooseImageIntent, "Select From");

    startImage.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            new Intent[]{takePictureIntent});
    ((Activity) mContext).startActivityForResult(startImage, Constants.REQUEST_CHOOSE_FROM);
} else {
    ((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}

Helpers.java

public static String LAST_IMAGE_FILE;
public static final String TEMP_IMAGE_FILE = "TEMP_IMG";

public static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "In.Touch");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + TEMP_IMAGE_FILE + "_" + timeStamp + ".jpg");
    LAST_IMAGE_FILE = mediaFile.getAbsolutePath();

    return mediaFile;
}

OnActivityResult

File file = new File(Helpers.LAST_IMAGE_FILE);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);

的AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

0 个答案:

没有答案