【问题标题】:share gif file from drawable folder从可绘制文件夹共享 gif 文件
【发布时间】:2018-01-29 03:23:38
【问题描述】:

我想分享位于可绘制文件夹中的 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);

        }
    });

谁能帮帮我,谢谢。

【问题讨论】:

  • 不,这行不通。你会收到一条错误消息。哪一个?
  • 无法附加空文件
  • 谁给出了这个错误?
  • 发送电子邮件并尝试所有方法,但无济于事
  • 请不要大喊大叫。写正常。

标签: java android android-intent share gif


【解决方案1】:

更改your package name -> 你的包名像这样com.example.test。您的设备必须像电报一样文件选择器,...

如果在模拟器中使用,则必须安装文件选择器,如 Telegram、ES 文件资源管理器。

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"));

【讨论】:

  • 再试一次。为我工作。
猜你喜欢
  • 1970-01-01
  • 2018-03-19
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 2020-09-14
  • 1970-01-01
相关资源
最近更新 更多