14

我想在图像视图中共享图像。但我不想保存到 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"));  

此处 Path 指定图像在 sdcard 中的位置

但我不想保存图像...有可能..

4

3 回答 3

28

The recommended method for sharing files with other apps is with a ContentProvider called FileProvider. The documentation is pretty good, but some parts are a little tricky. Here is a summary.

Set up the FileProvider in the Manifest

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

Replace com.example.myapp with your app package name.

Create 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>

This tells the FileProvider where to get the files to share (using the cache directory in this case).

Save the image to internal storage

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

Share the image

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

}

Further reading

于 2015-05-11T15:54:20.063 回答
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 上不起作用,文件必须在 file:///mnt/sdcard 上。]

于 2013-06-06T22:22:43.750 回答
2

您还可以将其作为媒体库内容提供者 URI 共享(如果图像已经在手机上,或者如果它来自网络,您可以共享 URL(尽管效果不同)。

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

于 2012-12-20T10:36:24.720 回答