通过Intent共享图像失败

时间:2015-05-26 07:58:03

标签: java android image android-intent android-camera

我正在尝试捕获图像,然后与应用分享。

max

但是当我运行此代码时,图像不会被共享。我的代码中有错误吗?

有没有其他方法可以共享图像而不将其保存在内存中?

2 个答案:

答案 0 :(得分:0)

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
try {
    InputStream stream = getContentResolver().openInputStream(screenshotUri);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using")); 

答案 1 :(得分:0)

我要感谢Alexander帮助找到解决方案。

这是工作代码 -

private static final int CAMERA_REQUEST = 1888;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
               .putExtra(Intent.EXTRA_STREAM,
                Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
                        (Bitmap) data.getExtras().get("data"), "title", null)
               ))
               .setType("image/jpeg");
        startActivity(intent);
    }
}