android原生分享图片失败的问题
16lz
2021-01-23
原生分享代码没有错,但是总是分享失败
Android分享图片的分享代码如下
public static void shareImages(Context context, ArrayList uriList){ Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.setType("image/*"); context.startActivity(shareIntent);}
其中uriList为你要分享的uri链接,就可以完成Android默认分享了。
上面的代码是没有问题的,但是为什么分享不成功呢?
于是乎,我用上面的代码分享一个本地的图片,分享成功了,但是为什么偏偏这个工程中分享失败呢,我就去找自己分享的图片,发现图片都是-xxxxxxxx的数字命名的。因为我分享的类型是.setType("image/*"),是个图片类型,但是这个缓存里面下载下来的不是图片的格式。
因为我的工程里面使用的是UniversalImageLoader加载图片的,这些图片缓存在本地文件夹下,但是他们都不是图片的文件格式,所以你在分享的时候总会分享失败。因此需要对UniversalImageLoader的缓存文件进行处理。
- 将缓存下来的图片重新命名:
public class UniversalImageLoaderConfiguration { public static void configure(Context context, int defaultImage) { configure(context, defaultImage, defaultImage, defaultImage); } public static void configure(Context context, int emptyUriImage, int failImage, int loadingImage) { DisplayImageOptions defaultOptions = (new DisplayImageOptions.Builder()).showImageForEmptyUri(emptyUriImage).showImageOnFail(failImage).showImageOnLoading(loadingImage).cacheInMemory(true).cacheOnDisk(true).build(); ImageLoaderConfiguration config = (new com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder(context)).defaultDisplayImageOptions(defaultOptions).writeDebugLogs().diskCacheFileNameGenerator(new UniversalImageLoaderImagePath()).build(); ImageLoader.getInstance().init(config); }}public class UniversalImageLoaderImagePath implements FileNameGenerator { @Override public String generate(String imageUri) { return String.valueOf(imageUri.hashCode()+".png"); }}
上面的代码是将缓存的图片文件以图片文件的形式保存在本地。
- 最后在application配置UniversalImageLoader
UniversalImageLoaderConfiguration.configure(getApplicationContext(), R.drawable.huise);
至此,解决了原生分享图片,图片分享失败的。
更多相关文章
- 【android】launcher图标自动生成、点9图片在线编辑工具
- Imageview图片圆形显示
- 代码设置RelativeLayout相对位置设置
- android调用图库获取图片显示在img中
- android瀑布流代码
- 【Android】监听SDCard安装和卸载的代码片段(测试通过)
- Android Base64转换图片,SimpleAdapter动态显示Bitmap图片