2014-10-20 10 views
5

Wenn ich Bild aus der Galerie wählen, wenn Bildgröße größer als 3 Mb android der OutOfMemoryError.OutOfMemoryError in BitmapFactory.decodeFile()

BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, options); 

Dieser Text aus Protokollen. Bitte helfen Sie mir, becouse "Frist")

E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.OutOfMemoryError 
     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623) 
     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378) 
     at com.DriverNotes.AndroidMobileClientTest.ProfileActivity.onActivityResult(ProfileActivity.java:104) 
     at android.app.Activity.dispatchActivityResult(Activity.java:5456) 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3402) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449) 
     at android.app.ActivityThread.access$1200(ActivityThread.java:150) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
+1

Sie haben zunächst das Bild zu skalieren. –

+0

Schreiben Sie Ihren Kommentar als Antwort und wenn diese Methode funktioniert - überprüfe ich Ihre Antwort als richtig – Artem

Antwort

22

OutofMemory tritt auf, wenn Sie Ihre App-Speicher in Heap zugewiesen überschreitet. Die Bitmap ist zu groß, um in den Speicher, dh in den Heap, zu passen. In diesem Fall haben Sie keinen Speicher mehr. Sie müssen die Bitmap verkleinern und dann dasselbe verwenden.

For that check the link below

diesen Code versuchen, Ihnen helfen können,

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ 
try { 
    //Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

    //The new size we want to scale to 
    final int REQUIRED_WIDTH=WIDTH; 
    final int REQUIRED_HIGHT=HIGHT; 
    //Find the correct scale value. It should be the power of 2. 
    int scale=1; 
    while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) 
     scale*=2; 

    //Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize=scale; 
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
} catch (FileNotFoundException e) {} 
return null; 
} 
+0

final int REQUIRED_WIDTH = WIDTH; final int REQUIRED_HIGHT = HÖHE; Ist das notwendig? –

4
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 8; 
Bitmap bm = BitmapFactory.decodeFile(path,options); 
Verwandte Themen