regelmäßig, können Sie schreiben nur BitmapFactory.decodeBitmap (....) usw., aber die Datei kann sehr groß sein, und Sie können das bekommen OutOfMemoryError in kürzester Zeit, vor allem, wenn Sie ein paar Mal in der Zeile Decodierung durchführen. Sie müssen also das Bild komprimieren, bevor Sie es zur Ansicht einstellen, damit Ihnen nicht der Speicher ausgeht. Hier ist der richtige Weg, es zu tun.
File f = new File(path);
if(file.exists()){
Bitmap myBitmap = ImageHelper.getCompressedBitmap(photoView.getMaxWidth(), photoView.getMaxHeight(), f);
photoView.setImageBitmap(myBitmap);
}
//////////////
/**
* Compresses the file to make a bitmap of size, passed in arguments
* @param width width you want your bitmap to have
* @param height hight you want your bitmap to have.
* @param f file with image
* @return bitmap object of sizes, passed in arguments
*/
public static Bitmap getCompressedBitmap(int width, int height, File f) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(f.getAbsolutePath(), options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(f.getAbsolutePath(), options);
}
/////////////////
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) >= reqHeight
&& (halfWidth/inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Sind Sie einige Fehler bekommen, wird die Datei in diesem Pfad existieren? überprüfe das: http://stackoverflow.com/questions/4181774/show-image-view-from-file-path –
Ich bekomme keine Fehler. Die Pfade sind korrekt. Ich bekomme die Bilder nicht. Nicht sicher, wenn ich die Bilder richtig setze –
Stellen Sie sicher, dass Sie Leseberechtigung haben: , stellen Sie auch sicher, dass die Datei nicht zu groß ist –