Picture got deleted after send via Google hangout

若如初见. 提交于 2019-12-13 04:29:44

问题


I have written a simple application to view pictures. However, after sending the picture with the common intent:

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("URLSTRING"));
        shareIntent.setType("image/jpeg");
        mActivity.startActivity(Intent.createChooser(shareIntent, "SHARE"));

The picture can be successfully sent if I chose Google hangout. But it is deleted after that!

Tested with other file manager application (root explorer) and it is the same behavior!

However, sent picture with GooglePlusGallery application does not seem to have this problem.

What is the trick? How to avoid the picture to be deleted?


回答1:


I had the same problem with Hangouts. It seems that it deletes the file even if it doesn't succeed to send it, thus always copy the file to a new temp file before calling the intent.




回答2:


I had the same issue, I believe exposing a ContentProvider instead that an uri could solve.




回答3:


Same problem here. My app didn't even have storage write permissions, so I'm assuming Hangouts actually is deleting the images.

So instead of sharing the file directly, i make a copy first. Here's the full solution:

Bitmap bm = BitmapFactory.decodeFile(filePath); // original image
File myImageToShare = saveToSD(bm); // this will make and return a copy
if (myImageToShare != null) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImageToShare));
    shareIntent.setType("image/jpeg");
    context.startActivity(Intent.createChooser(shareIntent, "Share using"));
}

private File saveToSD(Bitmap outputImage){
    File storagePath = new File(Environment.getExternalStorageDirectory() + yourTempSharePath);
    storagePath.mkdirs();

    File myImage = new File(storagePath, "shared.jpg");
    if(!myImage.exists()){
        try {
            myImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        myImage.delete();
        try {
            myImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        FileOutputStream out = new FileOutputStream(myImage);
        outputImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
        return myImage;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}


来源:https://stackoverflow.com/questions/21717591/picture-got-deleted-after-send-via-google-hangout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!