2017-06-03 7 views
0

私のアプリからfacebook、gmailに.gifを共有したいと思います。以下android:gif画像をdrawableから共有

ハローGIF画像

を共有する方法は、私は描画可能なフォルダ(「giphy.gif」)にGIFを持っている

は私がしようとしているコードですが、それは私にエラーを与える。(なし添付ファイル)

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     shareGif("giphy"); 

    } 


    private void shareGif(String resourceName) { 

     String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String fileName = "giphy.gif"; 

     File sharingGifFile = new File(baseDir, fileName); 

     try { 
      byte[] readData = new byte[1024 * 500]; 
      InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName())); 

      FileOutputStream fos = new FileOutputStream(sharingGifFile); 
      int i = fis.read(readData); 

      while (i != -1) { 
       fos.write(readData, 0, i); 
       i = fis.read(readData); 
      } 

      fos.close(); 
     } catch (IOException io) { 
     } 


     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     shareIntent.setType("image/gif"); 
     Uri uri = Uri.fromFile(sharingGifFile); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     startActivity(Intent.createChooser(shareIntent, "Share Emoji")); 

    } 




} 

答えて

1

まず追加の許可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

はその後、保存する必要がありますgifを外部ストレージに移動し、最後にそのファイルをロードします。

try { 
      CopyRAWtoSDCard(mContext,R.drawable.giphy, "giphy"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard,"/yourapp/giphy.gif"); 



      it.setType("image/*"); 
      it.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
      it.putExtra(Intent.EXTRA_SUBJECT, mContext.getString(R.string.app_name)); 
      it.putExtra(Intent.EXTRA_TEXT, "Gif attached"); 
      it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      Intent in = Intent.createChooser(it,"Share"); 
      in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      mContext.startActivity(in); 


private void CopyRAWtoSDCard(Context mContext,int id,String name) throws IOException { 


    String path = Environment.getExternalStorageDirectory() + "/yourapp"; 
    File dir = new File(path); 
    if (dir.mkdirs() || dir.isDirectory()) { 
     try { 

      InputStream in = mContext.getResources().openRawResource(id); 
      FileOutputStream out = new FileOutputStream(path+ File.separator + name+".gif"); 
      byte[] buff = new byte[1024]; 
      int read = 0; 
      try { 
       while ((read = in.read(buff)) > 0) { 
        out.write(buff, 0, read); 
       } 
      } finally { 
       in.close(); 
       out.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


} 

チャームのように機能します。

関連する問題