我正在从图库中选择多张图像,并且当我们清除最新的应用程序时,我们选择的图像也会被删除。即使从最近的应用程序中删除了图像,我也要显示图像,这意味着我想将图像保存在应用程序中。请提供代码。 您可以在此处查看屏幕截图以进行更多说明: enter image description here
通过单击图像,我可以从图库中选择图像,然后单击下面的按钮,我要将其保存在应用程序中。
这是我的主要活动。java
package com.amazoneindia.imageupload;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
ImageView iv1,iv2,iv3,iv4,iv5;
Button btn;
private final int CODE_IMG_GALLERY1=1;
private final int CODE_IMG_GALLERY2=2;
private final int CODE_IMG_GALLERY3=3;
private final int CODE_IMG_GALLERY4=4;
private final int CODE_IMG_GALLERY5=5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
init();
iv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("image/*"),"Select image"),
CODE_IMG_GALLERY1);
}
});
iv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("image/*"),"Select image"),
CODE_IMG_GALLERY2);
}
});
iv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("image/*"),"Select image"),
CODE_IMG_GALLERY3);
}
});
iv4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("image/*"),"Select image"),
CODE_IMG_GALLERY4);
}
});
iv5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("image/*"),"Select image"),
CODE_IMG_GALLERY5);
}
});
}
private void init(){
this.iv1=findViewById(R.id.D1ivss1);
this.iv2=findViewById(R.id.D1ivss2);
this.iv3=findViewById(R.id.D1ivss3);
this.iv4=findViewById(R.id.D1ivss4);
this.iv5=findViewById(R.id.D1ivss5);
this.btn=findViewById(R.id.btn);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CODE_IMG_GALLERY1&&resultCode==RESULT_OK){
Uri imageUri=data.getData();
if (imageUri!=null){
iv1.setImageURI(imageUri);
}
}
if (requestCode==CODE_IMG_GALLERY2&&resultCode==RESULT_OK){
Uri imageUri2=data.getData();
if (imageUri2!=null){
iv2.setImageURI(imageUri2);
}
}
if (requestCode==CODE_IMG_GALLERY3&&resultCode==RESULT_OK){
Uri imageUri3=data.getData();
if (imageUri3!=null){
iv3.setImageURI(imageUri3);
}
}
if (requestCode==CODE_IMG_GALLERY4&&resultCode==RESULT_OK){
Uri imageUri4=data.getData();
if (imageUri4!=null){
iv4.setImageURI(imageUri4);
}
}
if (requestCode==CODE_IMG_GALLERY5&&resultCode==RESULT_OK){
Uri imageUri5=data.getData();
if (imageUri5!=null){
iv5.setImageURI(imageUri5);
}
}
}
}
答案 0 :(得分:1)
好吧,问题在于您没有在任何地方保存图片网址,并且从最近删除应用程序时,变量也被清除了,因此解决方案是您可以将其保存在 SharedPreferences 中并获取任何内容时间在代码中的任何地方。
注意,从设置中清除应用程序数据也会从sharedPreferences中清除数据。
点击此链接
https://developer.android.com/training/data-storage/shared-preferences#java