Android 如何保存ImageButton的状态
我正在尝试将ImageButton的路径保存在savedInstanceState中,以避免旋转手机时丢失图像。但是只有一次在路径值i等于null(path==null)之后它才工作 这是我使用照相机或android gallery在imagebutton位图中设置的部分Android 如何保存ImageButton的状态,android,Android,我正在尝试将ImageButton的路径保存在savedInstanceState中,以避免旋转手机时丢失图像。但是只有一次在路径值i等于null(path==null)之后它才工作 这是我使用照相机或android gallery在imagebutton位图中设置的部分 switch(requestCode) { case REQUEST_CAMERA: if (resultCode == RESULT_OK) { final Bundle ex
switch(requestCode) {
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
final Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
if( bmp != null ) {
final File f = this.getFileStreamPath("ping_media.jpg");
if (f.exists()) {
f.delete();
}
try {
final FileOutputStream out = this.openFileOutput("ping_media.jpg",MODE_PRIVATE);
bmp.compress(CompressFormat.JPEG, 12, out);
out.close();
media_path = f.getAbsolutePath();
if(dm.densityDpi>=200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 72, 72), 72));
} else if(dm.densityDpi>130 && dm.densityDpi<200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 48, 48), 48));
} else {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 32, 32), 32));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break ;
case REQUEST_SELECT_PHOTO:
if( resultCode != 0 ) {
final Cursor c = managedQuery(data.getData(),null,null,null,null);
if (c.moveToFirst()) {
media_path = c.getString(1);
bmp = BitmapFactory.decodeFile(media_path);
contactimg.setImageBitmap(getRoundedCornerBitmap( getResizedBitmap(bmp, 48, 48), 48));
}
}
break;
}
开关(请求代码){
案件请求/摄像机:
if(resultCode==RESULT\u OK){
最终Bundle extras=data.getExtras();
bmp=(位图)附加获取(“数据”);
如果(bmp!=null){
最终文件f=this.getFileStreamPath(“ping_media.jpg”);
如果(f.exists()){
f、 删除();
}
试一试{
final FileOutputStream out=this.openFileOutput(“ping\u media.jpg”,MODE\u PRIVATE);
压缩(CompressFormat.JPEG,12,out);
out.close();
媒体路径=f.getAbsolutePath();
如果(干密度dpi>=200){
setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp,72,72),72));
}否则,如果(dm.densityDpi>130&&dm.densityDpi,则您似乎正在使用成员变量media_path来存储路径。在onCreate中再次读取捆绑包中的路径时,您需要更新此成员。否则,在onSaveInstanceState中尝试将其保存时,它仍然为空(同时运行开关块的情况除外)
switch(requestCode) {
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
final Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
if( bmp != null ) {
final File f = this.getFileStreamPath("ping_media.jpg");
if (f.exists()) {
f.delete();
}
try {
final FileOutputStream out = this.openFileOutput("ping_media.jpg",MODE_PRIVATE);
bmp.compress(CompressFormat.JPEG, 12, out);
out.close();
media_path = f.getAbsolutePath();
if(dm.densityDpi>=200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 72, 72), 72));
} else if(dm.densityDpi>130 && dm.densityDpi<200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 48, 48), 48));
} else {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 32, 32), 32));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break ;
case REQUEST_SELECT_PHOTO:
if( resultCode != 0 ) {
final Cursor c = managedQuery(data.getData(),null,null,null,null);
if (c.moveToFirst()) {
media_path = c.getString(1);
bmp = BitmapFactory.decodeFile(media_path);
contactimg.setImageBitmap(getRoundedCornerBitmap( getResizedBitmap(bmp, 48, 48), 48));
}
}
break;
}