Skip to content

Instantly share code, notes, and snippets.

@iniyanmurugavel
Last active May 17, 2020 13:55
Show Gist options
  • Select an option

  • Save iniyanmurugavel/99b051b4ffa05c9a4d87a4d9c2e865d5 to your computer and use it in GitHub Desktop.

Select an option

Save iniyanmurugavel/99b051b4ffa05c9a4d87a4d9c2e865d5 to your computer and use it in GitHub Desktop.
Android common errors
Question 1 :
Fatal Exception: java.lang.OutOfMemoryError:
Failed to allocate a 115252268 byte allocation with 16777216 free bytes and 90MB until OOM
OOM - OutOfMemoryError in java
solutions :
You also can set a larger memory heap witting android:largeHeap="true"
(OOM) in Java is one problem which is more due to system's limitation (memory)
rather than due to programming mistakes in most cases though in certain cases you could have memory leak
which causing OutOfMemoryError.
https://crunchify.com/how-to-generate-out-of-memory-oom-in-java-programatically/
Android ->
OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps.
This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated
due to lack of memory space and also, the garbage collector cannot free some space.
android:hardwareAccelerated="false" ,
android:largeHeap="true"
Example
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Add this tag in Application menifest file under "application" tag:
android:largeHeap="true"
String encodeFileToBase64Binary = encodeFileToBase64Binary(yourFile);
private String encodeFileToBase64Binary(File yourFile) {
int size = (int) yourFile.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(yourFile));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String encoded = Base64.encodeToString(bytes,Base64.NO_WRAP);
return encoded;
}
// Size image compress for image
public String resizeBase64Image(String base64image){
byte [] encodeByte=Base64.decode(base64image.getBytes(),Base64.DEFAULT);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable = true;
Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length,options);
if(image.getHeight() <= 400 && image.getWidth() <= 400){
return base64image;
}
image = Bitmap.createScaledBitmap(image, IMG_WIDTH, IMG_HEIGHT, false);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
System.gc();
return Base64.encodeToString(b, Base64.NO_WRAP);
}
Call this method, pass it the selectedUri, in return you will get the path that you want
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Failed to allocate a 2374572 byte allocation with 1676096 free bytes and 1636KB until OOM
Mi device issues
use in gradle
dexOptions {
incremental true
javaMaxHeapSize "4g"
preDexLibraries true
dexInProcess = true
}
1
Your app is crashing because your image size (in MB Or KB) is too large so it is not allocating space for that. So before pasting your image in drawable just reduce the size.
android:hardwareAccelerated="false"
android:largeHeap="true"
android:allowBackup="true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment