如何在安卓设备上保存和分享位图

3

我有一个带有图片查看器和共享按钮的活动,图片从数据库中检索出来。

我想先保存图片,然后再分享它。

一切都进行得很好,图像保存在内部存储器中,即将分享,但当其他应用程序运行时(如WhatsApp或Messaging),它会显示文件不受支持。

我已经手动推送其他图片到ddms,然后分享正常工作!O.o

我认为问题出在保存位图上,即使我检查了从ddms保存的位图,它看起来也很好。

这是我的代码:

save

  try {
        FileOutputStream out = new FileOutputStream(new File(getFilesDir(), "temp.png"));
        imageview.setImageBitmap(b1);
        b1.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
       }
       catch (Exception e) {}

共享方法

  private void initShareIntent(String type,String _text)
  {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(getFilesDir(), "temp.png")));
        shareIntent.setType("image/PNG");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, "send"));
  }

你是如何获取位图的? - Daniel Jonker
1
可能是[通过Android Intent共享位图]的重复问题(https://dev59.com/s1wX5IYBdhLWcg3w6jEE)。 - tripleee
1个回答

0

编辑这个

shareIntent.setType("image/*");

到这里

shareIntent.setType("image/png");

我认为它应该可以工作。如果它对你有用,请告诉我!

[编辑1] 更改为 png(而不是 PNG),因为我认为大小写很重要。哈哈

[编辑2] 对于多个图像,请尝试这个(由谷歌提供:P)

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

我认为在 MIME 类型中大小写是有影响的。 - SMR
没有问题出在这里。 我之前已经检查过所有的事情了,就像我所说的,它可以保存位图,但是无法共享。但是,如果我手动更改位图,它就能工作:| - Samad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接