我使用以下代码保存/加载我的图片
OnCreate(){
super.onCreate();
setContentView(...)
....
setImage();
}
protected void onResume(){
super.onResume();
setImage();
}
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
setImage();
}
protected void onPostResume(){
super.onPostResume();
setImage();
}
private void setImage(){
if(loadPicture(getIntent().getStringExtra("position"),bitmap ) != null){
Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(loadPicture(getIntent().getStringExtra("position"), bitmap));
}
}
private void takePicture() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(),
"Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 0);
}
private void savePicture(String filename, Bitmap b, Context ctx){
try {
ObjectOutputStream bos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
bos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
if(b.compress(Bitmap.CompressFormat.PNG, 100, bos) == true)
Toast.makeText(this, "returned true", Toast.LENGTH_LONG).show();
bos.flush();
// bos.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap loadPicture(String filename, Bitmap b){
//Drawable myImage = null;
try {
FileInputStream fis = openFileInput(filename);
ObjectInputStream bis = null;
try {
bis = new ObjectInputStream(fis);
} catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// myImage = Drawable.createFromStream(ois, filename);
b = BitmapFactory.decodeStream(bis);
try {
//bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//return myImage;
return b;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, bitmap.getHeight()/2, bitmap.getWidth()/2, false));
//previos imageView.setImageBitmap(bitmap)
savePicture(getIntent().getStringExtra("position"), bitmap,
getApplicationContext());
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
这非常适合在模拟器中保存图像(即样本图像),但它在真实手机中不起作用。拍摄图像并按下手机上的“确定”图标后,会出现一个空白屏幕约20秒,然后崩溃。
答案 0 :(得分:1)
您应该尝试一下这里提供的一些解决方案