安卓:保存图片到文件,并检索它 [英] Android: Saving Picture to a File and Retrieving it

查看:113
本文介绍了安卓:保存图片到文件,并检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在拍摄图片与我的相机,我想将其保存在该布局。我也想将其保存到一个文件,并能够加载图片时,我创建活动(所以如果我切换到不同的活动,然后回来这个)。截至目前,我可以拍摄照片并显示它,但如果我转活动一次以上,画面变得lost.I有以下相关code:

我打开我的图片OnCreate中使用 setImage()

 私人无效setImage(){
    如果(的LoadPicture(你好,位图)!= NULL){
        Toast.makeText(这一点,不空,Toast.LENGTH_SHORT).show();
        imageView.setImageBitmap(的LoadPicture(你好,位图));
    }
}

私人无效takePicture(){
    意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
    档案照片=
            新的文件(Environment.getExternalStorageDirectory(),Pic.jpg);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(照片));
    imageUri = Uri.fromFile(照片);
    startActivityForResult(意向,0);

}

公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);
        乌里selectedImage = imageUri;
            。getContentResolver()有NotifyChange(selectedImage,NULL);

            ContentResolver的CR = getContentResolver();

            尝试 {
                位= android.provider.MediaStore.Images.Media
                        .getBitmap(CR,selectedImage);

                imageView.setImageBitmap(Bitmap.createScaledBitmap(位图,bitmap.getHeight()/ 2,bitmap.getWidth()/ 2,假));
                // **当我将图片保存**
                savePicture(你好,位图,getApplicationContext());


     }

私人无效savePicture(字符串文件名,位图B,上下文CTX){
    尝试 {
        ObjectOutputStream的OOS;
        FileOutputStream中出; // =新的FileOutputStream(文件名);
        OUT = ctx.openFileOutput(文件名,Context.MODE_PRIVATE);
        OOS =新的ObjectOutputStream(出);
        b.com preSS(Bitmap.Com pressFormat.PNG,100,OOS);

        oos.close();
        oos.notifyAll();
        out.notifyAll();
        out.close();
    }赶上(例外五){
        e.printStackTrace();
    }
}

私人位图的LoadPicture(字符串文件名,位图B){
    //绘制对象MYIMAGE = NULL;
    尝试 {
        的FileInputStream FIS = openFileInput(文件名);
        ObjectInputStream的OIS = NULL;
        尝试 {
            OIS =新的ObjectInputStream(FIS);
        }赶上(StreamCorruptedException E1){
            e1.printStackTrace();
        }赶上(IOException异常E1){
            e1.printStackTrace();
        }
        // MYIMAGE = Drawable.createFromStream(OIS,文件名);
        B = BitmapFactory.de codeStream(OIS);
        尝试 {
            ois.close();
            fis.close();
        }赶上(IOException异常E){
            e.printStackTrace();
        }

    }赶上(FileNotFoundException异常E){
        e.printStackTrace();
    }
    //返回MYIMAGE;
    返回b;

}
 

解决方案

这听起来像你code ++工程,但你失去你的图像时,活动回来。加载你的照片,而不是的onCreate onPostResume()()可能是你所需要的。这感觉就像活动的生命周期的关键是你的问题。我有几个问题,我会投进的意见。

After taking a picture with my camera, I want to save it in that layout. I also want to save it to a File and be able to load that picture when I create the activity(so if i switch to a different activity and come back to this one). As of now, i can take the picture and display it, but if i switch activities more than once, the picture gets lost.I have the following relevant code:

I load my picture OnCreate using setImage():

private void setImage(){
    if (loadPicture("hello", bitmap) != null) {
        Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
        imageView.setImageBitmap(loadPicture("hello", 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);

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        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));
                //**Where I save the picture**
                savePicture("hello", bitmap, getApplicationContext());


     }

private void savePicture(String filename, Bitmap b, Context ctx){
    try {
        ObjectOutputStream oos;
        FileOutputStream out;// = new FileOutputStream(filename);
        out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(out);
        b.compress(Bitmap.CompressFormat.PNG, 100, oos);

        oos.close();
        oos.notifyAll();
        out.notifyAll();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private Bitmap loadPicture(String filename, Bitmap b){
    // Drawable myImage = null;
    try {
        FileInputStream fis = openFileInput(filename);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(fis);
        } catch (StreamCorruptedException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // myImage = Drawable.createFromStream(ois, filename);
        b = BitmapFactory.decodeStream(ois);
        try {
            ois.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // return myImage;
    return b;

}

解决方案

It sounds like your code works, but you are losing your image when the activity comes back. Loading your picture onPostResume() instead of onCreate() may be what you need. It feels like the activity lifecycle is the key to your problem. I have a couple of questions I will throw into comments.

这篇关于安卓:保存图片到文件,并检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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