使用Intent在Android上共享数据

时间:2014-08-03 09:22:04

标签: android image android-intent share

我在使用Intent在应用程序之间在Android上共享内容时遇到问题 我可以成功分享纯文本(在G +,Facebook和Facebook Messenger上) 代码如下:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Your score is: "+Integer.toString(mScore));
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

但是当谈到图像时,事情就不起作用了。 创建了Intent,我实际上可以在应用程序之间选择共享图像,但是当所选应用程序尝试启动时,它只会崩溃并且不会打开。 没有应用程序适用(使用9Gag,G +,Facebook,Facebook Messenger,照片测试..) 我正在使用的代码是:

Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

图像存储在      /data/data/com.test.carloalberto.myapp/files/score.png 这是我之前保存图像的正确路径。

为什么其他应用在尝试共享图片时会崩溃?

感谢您的回复。


编辑:我将图片存储在公共图片目录中,但像fb messenger这样的应用尝试共享它仍然会崩溃!

这是新代码:

File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = "score.jpg";

File file = new File(dir,name);
FileOutputStream ostream;

 try {
        if(!dir.exists()) {
           Log.i("Share: onClick()","Creating directory "+dir);
           dir.mkdirs();
        }

      Log.i("Share: onClick()","Final path: "+file.getAbsolutePath());
      file.createNewFile();

      ostream = new FileOutputStream(file);
      Log.i("onClick(): Share action: ","Path: "+file.getAbsolutePath());

      bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
      ostream.close();


      Uri uriToImage = Uri.parse(file.getAbsolutePath());
      Log.i("onClick(): Share action, final uri ",uriToImage.toString());

      Intent shareIntent = new Intent();
      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      shareIntent.setAction(Intent.ACTION_SEND);
      shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
      shareIntent.setType("image/jpeg");
      startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.send_to)));

  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e){
      e.printStackTrace();
  } catch (Exception e){
      e.printStackTrace();
  }

现在文件路径是:/mnt/sdcard/Pictures/score.jpg,它应该是公共的,并且可供所有其他应用程序访问。 (显然我也在清单文件中设置了权限)

2 个答案:

答案 0 :(得分:1)

应用程序无法使用

/data/*路径(每个* nix用户都应注意文件系统的安全模型)。

您可以在root设备上访问此文件夹并不意味着该应用可以执行相同操作。

您可以将图像存储在公共可用目录(例如设备的图片文件夹等)中,或尝试使用该方案为您的资产构建URI:

android.resource://[package]/[res type]/[res name]

答案 1 :(得分:0)

试试这个。

private void shareIntent(String type,String text){
     File filePath = getFileStreamPath("shareimage.jpg");
     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.putExtra(Intent.EXTRA_TEXT, text);
     shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));  

     shareIntent.setType("image/*"); //"image/*" or "image/jpeg", etc
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     startActivity(Intent.createChooser(shareIntent, "send"));
}