我有一个ArrayList
的自定义对象,它是图像对象,它将以字符串形式存储图像Uri。当我通过使用ArrayList Uri中的setImageUri()
显示图像时,它可以正确显示图像,但是使用ArrayList
将SharedPreferences
保存到Gson
之后,并且从SharedPreferences中获取了这个ArrayList之后,在onCreate()
中,它不在imageview中显示图像。
我调试了代码,从SharedPreferences正确获取了Uri,但是为什么它没有显示图像。
这是我的 GridViewActivity 代码:
public class GridViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
recyclerView=findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
autoFitGridLayoutManager=new AutoFitGridLayoutManager(context,250);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(autoFitGridLayoutManager);
arrayList=new ArrayList<>();
sharedPref=new SharedPref(context);
if (sharedPref.getCount()==0) {
Toast.makeText(context, "No images...", Toast.LENGTH_SHORT).show();
}
else {
Gson gson=new Gson();
String response=sharedPref.getImages();
arrayList=gson.fromJson(response,new TypeToken<ArrayList<Images>>(){}.getType()); //getting arraylist back
Log.d("ROHIT", String.valueOf(arrayList.size())); //it displays the arraylist size correctly
Images images=arrayList.get(0);
Log.d("ROHIT","aya: "+images.getImage()); //Uri is also correct
Intent intent=new Intent(context,Temp.class);
intent.putExtra("image",images.getImage()); //passing it to the Temp activity
startActivity(intent);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.add_imges_cam) {
//open camera
}
else if(item.getItemId()==R.id.add_imges_gallery) {
//open gallery to select images
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int count=0;
if(requestCode==PICK_IMAGE_MULTIPLE && resultCode==RESULT_OK) {
String[] filePath = {MediaStore.Images.Media.DATA};
if (data.getData() != null) {
//if single image is selected
Uri imageUri = data.getData();
Cursor cursor = getContentResolver().query(imageUri, filePath, null, null, null);
cursor.moveToFirst();
arrayList.add(new Images(imageUri.toString()));
cursor.close();
} else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePath, null, null, null);
// Move to first row
cursor.moveToFirst();
arrayList.add(new Images(uri.toString()));
count++;
cursor.close();
}
}
}
imagesAdapter=new ImagesAdapter(context,arrayList);
Images images=arrayList.get(0);
Intent intent=new Intent(context,Temp.class); //when i called Temp activity before saving arraylist to sharedPref it displays image correctly in Temp activity
intent.putExtra("image",images.getImage());
startActivity(intent);
sharedPref.save(arrayList); //saving arraylist to sharedPref
sharedPref.updateCount(count);
setRecyclerView();
}
}
}
这是我的 SharedPref 代码
public class SharedPref {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Context context;
public SharedPref(Context context) {
this.context=context;
sharedPreferences=context.getSharedPreferences("Grid_images",Context.MODE_PRIVATE);
editor=sharedPreferences.edit();
}
public void save(ArrayList<Images> arrayList) {
Gson gson=new Gson();
Object src;
String json=gson.toJson(arrayList);
editor.putString("grid_images",json);
editor.apply();
editor.commit();
}
public void updateCount(int i) {
editor.putInt("images_count",i);
editor.apply();
editor.commit();
}
public int getCount() {
return sharedPreferences.getInt("images_count",0);
}
public String getImages() {
return sharedPreferences.getString("grid_images",null);
}
public void clearData() {
editor.clear();
editor.commit();
}
}
这是临时类代码
public class Temp extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
imageView=findViewById(R.id.im_temp);
Log.d("ROHIT","inside tmep: "+getIntent().getStringExtra("image"));
imageView.setImageURI(Uri.parse(getIntent().getStringExtra("image")));
}
}
答案 0 :(得分:-1)
您是否在清单文件中添加了权限READ_EXTERNAL_STORAGE?