2012-03-31 12 views
2

Ich schreibe eine Anwendung, die die Kamera des Telefons verwendet, um ein Bild zu machen, und dann in meiner App verwenden. Die Sache ist, dass die App nicht mehr genügend Arbeitsspeicher hat, was wahrscheinlich an der hohen Auflösung der Bitmap liegt. Gibt es eine Möglichkeit, die Bitmap in der gleichen Größe zu halten, aber die Auflösung zu verringern?Ändern der Bitmap-Auflösung in Android App

Danke!

Antwort

2

diese Options.inSampleSize mit getan werden kann, wenn die Bitmap-Erstellung

+0

Ist das nicht für die Größe? Was, wenn Sie nur die Auflösung manipulieren möchten, ohne die Größe zu ändern? –

4

können Sie einstellen, seine Breite und Höhe

Bitmap bm = ShrinkBitmap(imagefile, 150, 150); 

Funktion

Bitmap ShrinkBitmap(String file, int width, int height){ 

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
    bmpFactoryOptions.inJustDecodeBounds = true; 
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

    if (heightRatio > 1 || widthRatio > 1) 
    { 
    if (heightRatio > widthRatio) 
    { 
     bmpFactoryOptions.inSampleSize = heightRatio; 
    } else { 
     bmpFactoryOptions.inSampleSize = widthRatio; 
    } 
    } 

    bmpFactoryOptions.inJustDecodeBounds = false; 
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
return bitmap; 
} 

}

Dies sind zwei weitere Links zu rufen, die Ihnen helfen könnten. Link 1 & Link 2

+0

das funktioniert, wenn ich die decodeResource Option bin mit zu bekomme meine Bitmap. Aber wenn ich von der get image intention arbeite, bekomme ich einen Uri und mache das: Uri selectedImageUri = data.getData(); Bitmap-Bitmap = MediaStore.Images.Media.getBitmap (this.getContentResolver(), selectedImageUri); Gibt es eine Möglichkeit, die Größe zu ändern, nachdem ich die Bitmap auf diese Weise habe? –

+0

Nein, ich denke nicht, dass es funktioniert ... – Bhavin

2

von jeet.chanchawat‘s Antwort: https://stackoverflow.com/a/10703256/3027225

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { 
     int width = bm.getWidth(); 
     int height = bm.getHeight(); 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     // CREATE A MATRIX FOR THE MANIPULATION 
     Matrix matrix = new Matrix(); 
     // RESIZE THE BIT MAP 
     matrix.postScale(scaleWidth, scaleHeight); 

     // "RECREATE" THE NEW BITMAP 
     Bitmap resizedBitmap = Bitmap.createBitmap(
      bm, 0, 0, width, height, matrix, false); 
     return resizedBitmap; 
    } 
Verwandte Themen