有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaAndroid:将图片保存到文件并检索它

用相机拍完照片后,我想把它保存在那个布局中。我还想将其保存到一个文件中,并在创建活动时能够加载该图片(因此,如果我切换到另一个活动并返回到此活动)。现在,我可以拍摄并显示照片,但如果我多次切换活动,照片就会丢失。我有以下相关代码:

我使用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("安卓.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 = 安卓.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;

}

共 (2) 个答案

  1. # 1 楼答案

    听起来你的代码是有效的,但当活动恢复时,你的形象就消失了。您可能需要在PostResume()而不是onCreate()上加载图片。感觉活动生命周期是解决问题的关键。我有几个问题要在评论中提出来

  2. # 2 楼答案

    out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
    

    尝试切换到:

    out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);