can't share image properly android

2019-06-12 15:21发布

If i share image one time in gmail and again come back and do some changes and again if i share then only previous image was displaying :( that was the issue with 4.1.1 when i run same problem in 4.2.2 it works perfectly :( :( :(

Hi i what to share image using Intent for that i have used below code

   public void otherBtn(View v) {
    FileOutputStream out = null;
    try {
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        File sdcard = Environment.getExternalStorageDirectory();
        File f = new File(sdcard, "temp.jpg");
        if (f.isFile()) {
            f.delete();
        }
        out = new FileOutputStream(f);
        captureRelativeLayout.setDrawingCacheEnabled(true);
        bitmap = captureRelativeLayout.getDrawingCache(true).copy(
                Config.ARGB_8888, false);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
        sharingIntent.setType("image/jpg");
        startActivity(sharingIntent);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

for example when i share below image with above code

enter image description here

the image which i have previously shared will shown but not above updated image is showing :(

enter image description here

1条回答
闹够了就滚
2楼-- · 2019-06-12 15:42

You flush and close the stream after starting the activity. You need to do it before to write the file contents properly.

out = new FileOutputStream(f);
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(
        Config.ARGB_8888, false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

// Flush and close stream
out.flush();
out.close();
// Now it's safe to use the file

sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
sharingIntent.setType("image/jpg");
startActivity(sharingIntent);
查看更多
登录 后发表回答