我正在编写一个应用程序来共享图像。我写了代码,但遇到了问题。我在做什么 1:将图像临时保存在内部存储 2:传递 URI 以共享图像。
图像已成功保存在内部存储中,但未在 whatsapp 上共享。当我在 whatsapp 上分享它时,Whatsapp 会打开,我选择收件人,whatsapp 处理它并说“重试”。
代码:
public void shareImage(View V)
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDownloadedImage.jpg");
try
{
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Uri downloadLocation=Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
startActivity(Intent.createChooser(share, "Share Image"));
}
图像成功保存在内部存储中,但未在 whatsapp 上共享。
截图如下。我们可以看到图像没有成功共享。