共享图像不起作用

时间:2015-01-12 07:51:41

标签: android

我有一个共享我刚刚保存到桌面的截图的功能,而文件肯定存在,并且共享活动正确加载,图像从不包含在内,为什么会这样?

private File lastScreenShot = null;

public void onShare(float top, float left, float width, float height){
    if(lastScreenShot == null){
        saveScreenShot(top, left, width, height);
    }
    if (!lastScreenShot.exists()) {
        return;
    }
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("image/jpeg");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, lastScreenShot);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, "Check out what I created!");
    startActivity(Intent.createChooser(sharingIntent, "Share Your Pretty Creation!"));
}

1 个答案:

答案 0 :(得分:1)

这是因为内容解析器不知道您的文件,因为您刚创建它。

将其插入数据库并按如下方式发送:

ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
values.put(MediaStore.Images.Media.DATA, lastScreenShot.getAbsolutePath());
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);  // Pass in a Uri not a File
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Check out what I created!");
startActivity(Intent.createChooser(sharingIntent, "Share Your Pretty Creation!"));

另外,请注意,在这种情况下,startActivity可以抛出android.content.ActivityNotFoundException