android:从drawable分享gif图片

时间:2017-06-03 08:09:28

标签: android

我想从我的应用程序分享.gif到facebook,gmail。

你好,有什么方法可以分享gif图片

我在可绘制文件夹中有gif(" giphy.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 个答案:

答案 0 :(得分:2)

首先添加权限

<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();
        }
    }


}

像魅力一样工作!