从drawable文件夹中共享gif文件

时间:2017-08-21 06:29:41

标签: java android android-intent share gif

我想分享位于drawable文件夹中的gif文件,我尝试了很多代码但是没有用。 我试过这个

void shareGif(String resourceName){

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

    File sharingGifFile = new File(baseDir, fileName);



   try {
        byte[] readData=new byte[1024*500];
        InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));
        Log.e("eeeee",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) {
       Log.e("eeeee",io.getMessage());
    }
    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"));
}

并在onclicklistner中调用它

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String name=getString(resource);
            shareGif(name);

        }
    });

任何人都可以帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

更改your package name - >您的包名称如此com.example.test。您的设备必须像电报一样提交选择器,......

如果在模拟器中使用,则必须安装文件选择器,如Telegram,ES file explorer。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.gif);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_TEXT, "Share gif file");
startActivity(Intent.createChooser(shareIntent, "Send your image"));