Moin,
in meiner App kann man ein Bild von Galeri oder Cam nehmen und in ein Imageview anzeigen lassen.
Nachdem man das Handy jedoch dreht, ist das Bild leider weg.
Habe jetzt etliche lösungen Probiert aber mir crasht entweder die App oder das Bild wird trotzdem gelöscht.
Hier ein ausschnitt wie ich es jetzt gemacht habe.
Das bild ist nach drehung leider immer noch weg.
ImageView viewImage;
Bitmap bitmap;
In onCreate:
if (savedInstanceState != null) {
bitmap = savedInstanceState.getParcelable("bitmap"
;
viewImage.setImageBitmap(bitmap);
}
In die Class:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("bitmap", bitmap);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg"
) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
if(bitmap!=null)
{
int photo_width = bitmap.getWidth();
int photo_height = bitmap.getHeight();
//1dp = 1.5 px 150dp = 225px
if(photo_width >230)
photo_width = 230;
if(photo_height > 140)
photo_height = 140;
bitmap = Bitmap.createScaledBitmap (bitmap, photo_width , photo_height , false);
}
viewImage.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"
;
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap bitmap = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image from gallery......******************.........", picturePath+""
;
if(bitmap!=null)
{
int photo_width = bitmap.getWidth();
int photo_height = bitmap.getHeight();
//1dp = 1.5 px 150dp = 225px
if(photo_width >230)
photo_width = 230;
if(photo_height > 140)
photo_height = 140;
bitmap = Bitmap.createScaledBitmap (bitmap, photo_width , photo_height , false);
}
viewImage.setImageBitmap(bitmap);
}
}
}
Was mache ich falsch?
Ich gebe bitmap eine feste Auflösung vom Bild und übergebe es dann in ImageView.
Mit outState.putParcelable("bitmap", bitmap); sichere ich mir mein bitmap aus ImageView und gebe es nach Handydrehung über if savedinstancestate != nuill wieder aus.
Liebe Grüße!