首页 > 解决方案 > 选择图像相机/画廊时出现内存不足异常

问题描述

我正在使用下面的代码通过相机/画廊挑选图像,并将它们的路径保存在 Sq Lite 中,这很好。一切正常。

我的问题是在选择图像 40 或更高版本后,出现内存不足异常。

有人可以帮忙吗?

从相机中选择图像:

try {
    String folder_main = "FOSImages";
    File f = new File(Environment.getExternalStorageDirectory(), folder_main);
    if (!f.exists()) {
        f.mkdirs();
    }
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f1 = new File(android.os.Environment.getExternalStorageDirectory(),
                "/" + folder_main + "/" + System.currentTimeMillis() + "_temp.jpg");
    fileName = System.currentTimeMillis() + "_temp.jpg";
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f1));
    startActivityForResult(intent, Constants.CAMERA_REQUEST_CODE);
} catch (Throwable throwable) {
    throwable.printStackTrace();
}

onCaptureImageResult:

try {
    File f = new File(Environment.getExternalStorageDirectory() + "/" + "FOSImages");
    for (File temp : f.listFiles()) {
        if (temp.getName().equals(fileName)) {
            f = temp;
            origionalPaths.add(f);
            if (imageType == 1) {
                stickerBeforeFileList.add(f);
                horizentalAdapter1.notifyDataSetChanged();
            } else {
                stickerAfterFileList.add(f);
                horizentalAdapter2.notifyDataSetChanged();
            }
        }
    }

} catch (Throwable throwable) {
    throwable.printStackTrace();
}

从图书馆挑选:

try {
    Intent intent = new Intent(
            Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    intent.setType("image/*");
    startActivityForResult(
            Intent.createChooser(intent, "Select File"),
            Constants.GALLARY_REQUEST_CODE);

} catch (Throwable throwable) {
    throwable.printStackTrace();
}

onSelectFromGalleryResult:

try {
    Uri selectedImageUri = data.getData();
    File shopImageFile1 = new 
 File(Utilities.getRealPathFromURI(selectedImageUri, UberFormActivity.this));
    if (imageType == 1) {
        stickerBeforeFileList.add(carImageFile1);
        horizentalAdapter1.notifyDataSetChanged();
    } else {
        stickerAfterFileList.add(carImageFile2);
        horizentalAdapter2.notifyDataSetChanged();
    }
} catch (Throwable throwable) {
    throwable.printStackTrace();
}

标签: androidout-of-memoryandroid-camera

解决方案


推荐阅读