在ImageView中安装android共享图像而不保存在SD卡中

时间:2012-12-20 10:30:36

标签: android bitmap

我想在图片视图中分享图片。但我不想保存到SD卡。 但是当我使用Intent分享我用过的代码时

Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
                share.putExtra(Intent.EXTRA_STREAM,Uri.parse(path));
                startActivity(Intent.createChooser(share, "Share Image"));  

此处路径指定了sdcard中图像的位置

但我不想要保存图片...有可能......

3 个答案:

答案 0 :(得分:19)

与其他应用共享文件的推荐方法是使用名为ContentProviderFileProvider。文档非常好,但有些部分有点棘手。以下是摘要。

在清单

中设置FileProvider
<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
</manifest>

com.example.myapp替换为您的应用包名称。

创建res / xml / filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="images/"/>
</paths>

这告诉FileProvider在哪里获取要共享的文件(在这种情况下使用缓存目录)。

将图像保存到内部存储

// save bitmap to cache directory
try {

    File cachePath = new File(context.getCacheDir(), "images");
    cachePath.mkdirs(); // don't forget to make the directory
    FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    stream.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

分享图片

File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);

if (contentUri != null) {

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
    shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    startActivity(Intent.createChooser(shareIntent, "Choose an app"));

}

进一步阅读

答案 1 :(得分:5)

我能够做到这一点:

File file = new File( getCacheDir(), "screenshot.png");
...
file.setReadable(true, false);
Uri uri = Uri.fromFile(file);
...
intent.putExtra(Intent.EXTRA_STREAM, uri);

这样我将文件保存在我自己的缓存文件夹中,所以我不会愚弄任何公共文件夹或取决于存在的SD卡。

此外,这种方式会在用户删除我的应用时自动删除,但我也使用startActivityForResult / onActivityResult删除文件,并在共享完成后将其文件夹设置为私有。

(我个人希望找到一种共享比特流的方法,避免完全创建文件的步骤,但我认为这不可行。)

[编辑:我发现这不适用于2.3.6文件必须存档的文件:/// mnt / sdcard。]

答案 2 :(得分:1)

您也可以将其作为媒体库内容提供商URI共享(如果图片已经在手机上,或者如果它来自网络,您可以共享该网址(虽然效果不同)。

但是如果来自网络并且您直接解码为Bitmap并且现在想要将其作为正确的图像分享,是的,您真的需要该文件!