选取相机/图库图像时出现内存不足异常

时间:2018-07-03 05:26:44

标签: android out-of-memory android-camera

我正在使用以下代码通过“相机/画廊”选择图像,并将其路径保存在Sq Lite中。一切正常。

我的问题是选择40张或以上的图像后,出现内存不足的异常。

可以帮忙吗?

chooseImageFromCamera:

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();
}

choiceFromLibrary:

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();
}

0 个答案:

没有答案