I'm trying to save file taken by another app into internal directory at background thread with this method :
public static File saveUri(Uri uri,File file, WeakReference<Context> contextWeakReference) throws IOException {
ContentResolver resolver=contextWeakReference.get().getContentResolver();
InputStream inputStream=resolver.openInputStream(uri);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
FileOutputStream fileOutputStream=new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
stream.write(buffer, 0, bytesRead);
}
fileOutputStream.write(stream.toByteArray());
fileOutputStream.close();
inputStream.close();
return file ;
}
and it works until I save file like 100 MB size. Above that it gives :
java.lang.OutOfMemoryError: Failed to allocate a 134217744 byte allocation with 25165824 free bytes and 124MB until OOM, max allowed footprint 95471864, growth limit 201326592
at this line :
stream.write(buffer, 0, bytesRead);
This is what I use for backround process :
public static final ExecutorService databaseWriteExecutor =
Executors.newSingleThreadExecutor();
What should I do for saving big size file without causing Out Of Memory Error.