如何将图像视图传递给另一个布局并将其保存在布局中?

How to pass image View to another layout and save it inside the layout?

我能够将图像视图传递到另一个布局,但是一旦我关闭应用程序或更改布局并返回到具有传递的图像视图的布局。图像视图消失。我的问题是如何将图像视图也保留在它传递的布局中?这是我在网上找到的传递图像视图。

FirstClass.java

RandomImageHere.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getApplicationContext(), SecondClass.class);

            intent.putExtra("resourseInt", R.drawable.picture);
            startActivity(intent);

        }
    });

SecondClass.java

private ImageView imageView;

Bundle extras = getIntent().getExtras();
  imageView = (ImageView) findViewById(R.id.image_view);

    if (extras == null)
    {
        return;
    }
    int res = extras.getInt("resourseInt");
    imageView.setImageResource(res);

SecondClass.xml

<ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/image_view"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

和接收者Activity

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

从Intent中提取出来的图片资源可以保存在SharedPreferences中

PreferenceManager.getDefaultSharedPreferences(this).edit()
            .putInt("iv", res).commit();

然后在 onResume() 方法中

int r = PreferenceManager.getDefaultSharedPreferences(this)
.getInt("iv", R.mipmap.ic_launcher);

imageView.setImageResource(r);

您可以将您的资源 ID 保存到 sharedpreference:

private ImageView imageView;
SharedPreference savedImage;

在您的 OnCreate 方法中:

OnCreate(){
.....savedImage = PreferenceManager.getDefaultSharedPreferences(this);
}

然后根据偏好设置图像,如果它包含:

Bundle extras = getIntent().getExtras();
  imageView = (ImageView) findViewById(R.id.image_view);

    if (extras == null)
    {
        return;
    }
    else{
        int res = extras.getInt("resourseInt");
        savedImage.edit().putInt("ImageID", res).apply();
        if(savedImage.contains("ImageID"){
            imageView.setImageResource(savedImage.getInt("ImageId", 0));
        }
    }

I was able to pass image view to another layout but once I close the app or change the layout and go back to the layout with the passed image view. The image view disappears.

您采用了错误的解决方案。如果您将数据从 Activity FirstClass 传递到 -> SecondClass 并要求下次在 FirstClass 不知情的情况下访问该数据,那么您应该将该特定信息保存在存储中.您可以为此使用 SharedPreferences,这是您的操作方式:

在你的 FirstClass 中:

RandomImageHere.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        SharedPreferences pref = getSharedPreferences("Images", Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        ed.putInt("IMG", R.drawable.picture);
        ed.apply();
        Intent intent = new Intent(getApplicationContext(), SecondClass.class);
        startActivity(intent);

    }
});

然后在你的 SecondClass 中:

    private ImageView imageView;
    imageView = (ImageView) findViewById(R.id.image_view);
    SharedPreferences pref = getSharedPreferences("Images", Context.MODE_PRIVATE);
    int res = pref.getInt("IMG",0);
    if(res!=0)
    {
        imageView.setImageResource(res);

    }

在将其添加到意图、发送出去和解码之前将其转换为字节数组。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);

然后在其他activity下面写行

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);`enter code here`