I am using the following code to save/ load my picture
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.开发者_JAVA百科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();
}
This works perfectly for saving the image in the emulator(the sample image, that is), but it doesn't work in the real phone. After I take the image and press the 'OK" icon on the phone, there is a blank screen for about 20 secs. and then it crashes.
You should try some of the solutions provided here
- Strange out of memory issue while loading an image to a Bitmap object
精彩评论