图片保存在模拟器但不是在装置 [英] Picture saving in emulator but not on device

查看:111
本文介绍了图片保存在模拟器但不是在装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code保存/载入我的照片

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.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();



            }

这完全适用于在模拟器(样本图像,这是)保存图像,但它并没有在真正的手机工作。我拿的形象和preSS手机上的OK图标后,有一个空白屏幕约20秒,然后崩溃。

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

  • Android: Strange out of memory issue

这篇关于图片保存在模拟器但不是在装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆