【问题标题】:Share image via other apps in android通过 android 中的其他应用共享图像
【发布时间】:2017-01-14 09:50:41
【问题描述】:

我已成功检索图像并将其显示在列表中,但是当我共享它时,它显示“无法附加空文件”

这是我尝试过的

  holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String PATH = "/mnt/sdcard/"+ listItem.getImg();
            File f = new File(PATH);
            Uri yourUri = Uri.fromFile(f);
         //   Uri contenturi= FileProvider.getUriForFile(context," com.astu360.bjp2017",f);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("Image/*");
            intent.putExtra(Intent.EXTRA_STREAM, yourUri);
            intent.putExtra(Intent.EXTRA_TEXT,"These are the content..");
            context.startActivity(Intent.createChooser(intent,"Share via.."));
        }
    });

任何帮助和建议将不胜感激。

【问题讨论】:

  • 您是否尝试将图像作为 base64 字符串嵌入到额外内容中?下周我将不得不对 Keystore 做同样的事情。
  • @Xvolks 怎么做...??你能分享那部分代码吗..我会感谢你

标签: java android file android-intent uri


【解决方案1】:

你应该使用位图压缩并尝试选择器。

Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 150, bytes);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "file.jpg");
try {
    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bytes.toByteArray());
} catch (IOException e) {                       
        e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/file.jpg"));
startActivity(Intent.createChooser(share, "Share..."));

【讨论】:

  • 如果我有多种类型的图像怎么办
  • codetheory.in/… 这会对你有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
相关资源
最近更新 更多