无法正常分享图片android

时间:2014-02-28 09:27:06

标签: android android-intent

如果我在gmail中共享图像一次并再次回来并做一些更改,如果我分享然后只显示以前的图像:(当我在4.2中运行相同的问题时,这是4.1.1的问题.2它完美无缺:( :( :(

嗨,我使用Intent分享图像,因为我使用了以下代码

   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();
            }

        }
    }
}

例如,当我与上面的代码共享下面的图像时

enter image description here

我之前分享过的图片会显示,但上面没有更新的图片显示:(

enter image description here

1 个答案:

答案 0 :(得分:0)

您在启动活动后刷新并关闭流。你需要在之前正确地编写文件内容。

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);