Android:其他应用读取共享图像时出现问题
Android: problems reading shared image by other apps
我正在开发一个使用从其他应用(例如图库、浏览器)共享的图像的应用。
代码:
public void handleImage() {
Intent intent = getIntent();
Uri imgUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imgUri != null){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
InputStream imgInputStream = context.getContentResolver().openInputStream(imgUri);
Bitmap img = (BitmapFactory.decodeStream(imgInputStream));
img.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imgByteArray = stream.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("IMAGE_BYTEARRAY", imgByteArray);
FragmentEntityEdit fragmentEntityEdit = new FragmentEntityEdit();
fragmentEntityEdit.setArguments(bundle);
changeFragment(fragmentEntityEdit,true,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
当我第一次从浏览器分享图片时,效果很好。片段启动,图像可以加载到 ImageView。
但是我第二次使用浏览器的共享选项时,它没有加载新图像。 (仅当我手动清除应用程序数据时。)
更新(在 fragmentEntityEdit 中加载 ImageView 图像):
在 OnCreateView 中:
Bundle bundle = getArguments();
if (bundle != null) {
imgByteArray = bundle.getByteArray("IMAGE_BYTEARRAY");
}
ImageView imgOfEntity = view.findViewById(R.id.imageview_imgofentity);
if (imgByteArray != null) {
imgOfEntity.setImageBitmap(null);
Glide
.with(getActivity())
.load(imgByteArray)
.into(imgOfEntity);
}
你知道我怎样才能找到更新的图片吗?
从您发布的代码来看,问题与您的 Glide
通话有关。
Glide
没有将新请求加载到您的 ImageView
,因为它的缓存。
为了修复它,您可以尝试这样的操作:
Glide.with(imgOfEntity.getContext())
.load(imgByteArray)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imgOfEntity);
我正在开发一个使用从其他应用(例如图库、浏览器)共享的图像的应用。
代码:
public void handleImage() {
Intent intent = getIntent();
Uri imgUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imgUri != null){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
InputStream imgInputStream = context.getContentResolver().openInputStream(imgUri);
Bitmap img = (BitmapFactory.decodeStream(imgInputStream));
img.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imgByteArray = stream.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("IMAGE_BYTEARRAY", imgByteArray);
FragmentEntityEdit fragmentEntityEdit = new FragmentEntityEdit();
fragmentEntityEdit.setArguments(bundle);
changeFragment(fragmentEntityEdit,true,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
当我第一次从浏览器分享图片时,效果很好。片段启动,图像可以加载到 ImageView。 但是我第二次使用浏览器的共享选项时,它没有加载新图像。 (仅当我手动清除应用程序数据时。)
更新(在 fragmentEntityEdit 中加载 ImageView 图像): 在 OnCreateView 中:
Bundle bundle = getArguments();
if (bundle != null) {
imgByteArray = bundle.getByteArray("IMAGE_BYTEARRAY");
}
ImageView imgOfEntity = view.findViewById(R.id.imageview_imgofentity);
if (imgByteArray != null) {
imgOfEntity.setImageBitmap(null);
Glide
.with(getActivity())
.load(imgByteArray)
.into(imgOfEntity);
}
你知道我怎样才能找到更新的图片吗?
从您发布的代码来看,问题与您的 Glide
通话有关。
Glide
没有将新请求加载到您的 ImageView
,因为它的缓存。
为了修复它,您可以尝试这样的操作:
Glide.with(imgOfEntity.getContext())
.load(imgByteArray)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imgOfEntity);