【发布时间】:2017-03-11 03:00:01
【问题描述】:
我正在开发一个 Android 应用程序,其中我必须实现与其他消息传递应用程序共享 GIF 的功能。
为此,我将其存储在一个临时文件中并通过意图传递文件的 url。我的 gif 存储为可绘制对象。因此,在检索它时,我使用了 FileOutputStream 和 Bitmap.Compress。由于这个而不是GIF,我得到了一个静止图像。
另外,我尝试在不压缩的情况下将位图转换为字节数组,但在这种情况下,我的图像没有显示出来
我分享 gif 的代码:
int drawableId = gridItemList.get(position);
Bitmap contentToShare = BitmapFactory.decodeResource(getResources(), drawableId);
try {
File cachePath=new File(getApplicationContext().getFilesDir(),"content");
cachePath.mkdirs();
FileOutputStream stream=new FileOutputStream(cachePath+"/anim.gif");
contentToShare.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
File imagePath = new File(getApplicationContext().getFilesDir(), "content");
File newFile = new File(imagePath,"anim.gif");
Uri contentURI=FileProvider.getUriForFile(getApplicationContext(),getPackageName()+".fileprovider",newFile);
if(contentURI!=null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentURI);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("image/gif");
startActivity(shareIntent);
}
}catch (Exception e) {
Log.e("Exception",e.toString());
}
PS : 共享 jpeg 和 png 效果很好。
【问题讨论】:
-
发布您的代码...
-
使用 Glide 库来展示你的 GIF
-
我已经在使用 glide 从 drawable 加载 gif。但我无法分享 gif