Android 如何将图像保存在内部存储器中,并在另一个活动中的imageView中显示图像

Android 如何将图像保存在内部存储器中,并在另一个活动中的imageView中显示图像,android,android-imageview,Android,Android Imageview,如何将图像保存在内部存储器中并在另一个活动的imageView中显示图像,请告诉我如何将图像保存在内部存储器中,因为许多手机只有内部存储器而没有sd卡 这是我的第一次活动 这是我的第二项活动 我是存储在模拟器内部存储下面使用的代码图像 private String saveToInternalStorage(Bitmap bitmapImage){ ContextWrapper cw = new ContextWrapper(getApplicationContext());

如何将图像保存在内部存储器中并在另一个活动的imageView中显示图像,请告诉我如何将图像保存在内部存储器中,因为许多手机只有内部存储器而没有sd卡

这是我的第一次活动 这是我的第二项活动
我是存储在模拟器内部存储下面使用的代码图像

private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
   // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    } 
    return directory.getAbsolutePath();
}
    private void OpenCamera(){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap thumbnail = null;

    if (resultCode==RESULT_OK){
        if (requestCode==CAMERA_REQUEST_CODE)
        {
                thumbnail = (Bitmap) data.getExtras().get("data");
                Intent intent = new Intent(this, DbInsert.class);
                intent.putExtra("name", thumbnail);
                startActivity(intent);
            }

    }
}

在opencamera方法中进行更改,如下面使用的代码

private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
   // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    } 
    return directory.getAbsolutePath();
}
    private void OpenCamera(){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap thumbnail = null;

    if (resultCode==RESULT_OK){
        if (requestCode==CAMERA_REQUEST_CODE)
        {
                thumbnail = (Bitmap) data.getExtras().get("data");
                Intent intent = new Intent(this, DbInsert.class);
                intent.putExtra("name", thumbnail);
                startActivity(intent);
            }

    }
}
第二项活动

        Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
    imageView.setImageBitmap(bitmap);

您的代码有什么问题?您面临的确切问题是什么..了解碎片首选项它可以帮助您存储数据或检索,也可以将图像保存在存储器中,然后将图像加载到任意位置@VladyslavMatviienko捕获图像后,保存图像后,当我从相机返回时,我进入第一个活动(当前活动)不去第二个活动它意味着uri变为null。那么什么解决方案检查你的onActivity结果方法你的数据给null值。我知道我的数据给null值这就是为什么我问这个问题,所以请给我解决方案