android 设备重启时 ImageView 不保留图像
ImageView not retaining Image when android device restart
我从图库中选择的图像显示在 ImageView 中,但是当 android 设备重启时没有保留或保存图像我需要再次重新选择图像。我的计划是即使设备重新启动,图像仍保留在图像视图中,或者我是否需要创建一些数据来保存图像并显示到 ImageView
public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
private Uri mImageUri;
@Override
public void onResume() {
super.onResume();
}
private File mSnapFile;
private static final String ARG_URI_IMAGE_1 = "image1Uri";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
imageButton1.setOnClickListener(this::onClick);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String mImageUri = preferences.getString("image", null);
if (mImageUri != null) {
imageButton1.setImageURI(Uri.parse(mImageUri));
}
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.firstimagebtn:
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
if (data != null) {
// This is the key line item, URI specifies the name of the data
mImageUri = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image", String.valueOf(mImageUri));
editor.commit();
// Sets the ImageView with the Image URI
imageButton1.setImageURI(mImageUri);
imageButton1.invalidate();
}
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){
// This is the key line item, URI specifies the name of the data
mImageUri2 = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image2", String.valueOf(mImageUri2));
editor.commit();
// Sets the ImageView with the Image URI
imageButton2.setImageURI(mImageUri2);
imageButton2.invalidate();
}
break;
}
}
您可以使用 Glide 或 Picasso 等图像加载库。他们提供数据缓存,您的图像将被保留。
默认情况下,Glide 在开始对图像的新请求之前检查多层缓存:
1.Active 资源 - 此图像现在是否显示在另一个视图中?
2.Memory 缓存 - 此图像是最近加载的并且仍在内存中吗?
3.Resource - 此图像之前是否被解码、转换并写入磁盘缓存?
4.Data - 这张图片的数据是之前写入磁盘缓存的吗?
前两个步骤检查资源是否在内存中,如果是,则立即 return 图像。后两步检查图像是否在磁盘上并 return 快速但异步。
如果这四个步骤都找不到图片,那么Glide会回到原始源检索数据(原始File,Uri,Url等)。
希望这对您有所帮助。如果您对此有任何疑问,请告诉我,但我认为文档几乎是不言自明的。
我从图库中选择的图像显示在 ImageView 中,但是当 android 设备重启时没有保留或保存图像我需要再次重新选择图像。我的计划是即使设备重新启动,图像仍保留在图像视图中,或者我是否需要创建一些数据来保存图像并显示到 ImageView
public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
private Uri mImageUri;
@Override
public void onResume() {
super.onResume();
}
private File mSnapFile;
private static final String ARG_URI_IMAGE_1 = "image1Uri";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
imageButton1.setOnClickListener(this::onClick);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String mImageUri = preferences.getString("image", null);
if (mImageUri != null) {
imageButton1.setImageURI(Uri.parse(mImageUri));
}
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.firstimagebtn:
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
if (data != null) {
// This is the key line item, URI specifies the name of the data
mImageUri = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image", String.valueOf(mImageUri));
editor.commit();
// Sets the ImageView with the Image URI
imageButton1.setImageURI(mImageUri);
imageButton1.invalidate();
}
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){
// This is the key line item, URI specifies the name of the data
mImageUri2 = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image2", String.valueOf(mImageUri2));
editor.commit();
// Sets the ImageView with the Image URI
imageButton2.setImageURI(mImageUri2);
imageButton2.invalidate();
}
break;
}
}
您可以使用 Glide 或 Picasso 等图像加载库。他们提供数据缓存,您的图像将被保留。
默认情况下,Glide 在开始对图像的新请求之前检查多层缓存:
1.Active 资源 - 此图像现在是否显示在另一个视图中?
2.Memory 缓存 - 此图像是最近加载的并且仍在内存中吗?
3.Resource - 此图像之前是否被解码、转换并写入磁盘缓存?
4.Data - 这张图片的数据是之前写入磁盘缓存的吗?
前两个步骤检查资源是否在内存中,如果是,则立即 return 图像。后两步检查图像是否在磁盘上并 return 快速但异步。
如果这四个步骤都找不到图片,那么Glide会回到原始源检索数据(原始File,Uri,Url等)。
希望这对您有所帮助。如果您对此有任何疑问,请告诉我,但我认为文档几乎是不言自明的。