2016-10-28 42 views
-4

我正在開發一個Android應用程序,其中我必須實現與其他消息應用程序共享GIF的功能。在Android中共享GIF

爲此,我將存儲在臨時文件中並通過意向傳遞文件的URL。我的gif存儲爲可繪製的。因此,雖然檢索它,我用Bitmap.Compress FileOutputStream。由於這個而不是GIF,我得到一個stil圖像。

而且,我試圖將位圖字節數組而不壓縮,但在這種情況下,我的圖片沒有顯示出來

我的代碼共享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是完美地工作。

+0

發佈您的代碼... –

+0

使用滑翔庫,以顯示您的GIF –

+0

我已經使用glide從drawable加載gif。但我無法分享gif –

回答

0

使用滑翔庫,以顯示你的GIF

在您的搖籃:

compile 'com.github.bumptech.glide:glide:3.7.0' 

寫代碼:

String gifUrl = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif"; 

Glide 
    .with(context) 
    .load(gifUrl) 
    .into(imageViewGif);