Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent createExternalStoragePrivateFile from OutOfMemory crashes/ANR's #1998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,18 @@ private File createExternalStoragePrivateFile(Context context, Uri uri) throws F
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// no error checking. Note that if external storage is
// not currently mounted this will silently fail.
OutputStream outputStream = new FileOutputStream(file);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
outputStream.write(data);
FileOutputStream fileOutputStream = new FileOutputStream(file);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len;
while ((len = inputStream.read(buffer, 0 , bufferSize)) != -1) {
fileOutputStream.write(buffer, 0, len);
fileOutputStream.flush();
}
inputStream.close();
outputStream.close();
fileOutputStream.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Expand Down Expand Up @@ -873,13 +876,17 @@ private void croppingResult(Activity activity, final int requestCode, final int

@Override
public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
if (requestCode == IMAGE_PICKER_REQUEST) {
imagePickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == CAMERA_PICKER_REQUEST) {
cameraPickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == UCrop.REQUEST_CROP) {
croppingResult(activity, requestCode, resultCode, data);
}
Runnable backgroundRunnable = () -> {
if (requestCode == IMAGE_PICKER_REQUEST) {
imagePickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == CAMERA_PICKER_REQUEST) {
cameraPickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == UCrop.REQUEST_CROP) {
croppingResult(activity, requestCode, resultCode, data);
}
};

new Thread(backgroundRunnable).start();
}

@Override
Expand Down