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

Sel*_*pan 11 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"));  
Run Code Online (Sandbox Code Playgroud)

这里路径指定了SD卡中图像的位置

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

Sur*_*gch 21

与其他应用程序共享文件的推荐方法是使用名为FileProviderContentProvider.文档非常好,但有些部分有点棘手.这是一个总结.

在Manifest中设置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>
Run Code Online (Sandbox Code Playgroud)

替换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>
Run Code Online (Sandbox Code Playgroud)

这告诉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();
}
Run Code Online (Sandbox Code Playgroud)

分享图片

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

}
Run Code Online (Sandbox Code Playgroud)

进一步阅读

  • 对于 AndroidX - 使用“androidx.core.content.FileProvider” (2认同)

Tom*_*Tom 5

我能够做到这一点:

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

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

此外,这样它就会自动在用户删除我的应用程序删除,但我还用startActivityForResult/onActivityResult删除的文件,当份额完成设定的文件夹回给私人.

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

[编辑:我发现这不适用于2.3.6文件必须在文件:/// mnt/sdcard.]


Bud*_*ius 2

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

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

  • 我不敢相信如果不将图像保存到文件中就没有办法做到这一点! (2认同)