位图的 Android 共享意图 - 是否可以在共享之前不保存它? [英] Android Share Intent for a Bitmap - is it possible not to save it prior sharing?

查看:14
本文介绍了位图的 Android 共享意图 - 是否可以在共享之前不保存它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用共享意图从我的应用程序导出位图,而不为临时位置保存文件.我找到的所有例子都是两步走1)保存到SD卡并为该文件创建Uri2) 用这个 Uri 开始意图

I try to export a bitmap from my app using share intent without saving a file for a temporal location. All the examples I found are two-step 1) save to SD Card and create Uri for that file 2) start the intent with this Uri

是否可以在不需要 WRITE_EXTERNAL_STORAGE 权限的情况下制作它,保存文件 [然后删除它]?如何在没有 ExternalStorage 的情况下寻址设备?

Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]? How to address devices without ExternalStorage?

推荐答案

我遇到了同样的问题.我不想要求读取和写入外部存储权限.此外,有时当手机没有 SD 卡或卡被卸载时会出现问题.

I had this same problem. I didn't want to have to ask for the read and write external storage permissions. Also, sometimes there are problems when phones don't have SD cards or the cards get unmounted.

以下方法使用一个名为 ContentProviderContentProviderhttp://developer.android.com/reference/android/support/v4/content/FileProvider.html" rel="noreferrer">FileProvider.从技术上讲,您仍然在共享之前保存位图(在内部存储中),但您不需要请求任何权限.此外,每次共享位图时,图像文件都会被覆盖.并且由于它在内部缓存中,所以当用户卸载应用程序时它会被删除.所以在我看来,这和不保存图像一样好.这种方法也比保存到外部存储更安全.

The following method uses a ContentProvider called FileProvider. Technically, you are still saving the bitmap (in internal storage) prior to sharing, but you don't need to request any permissions. Also, every time you share the bitmap the image file gets overwritten. And since it is in the internal cache, it will be deleted when the user uninstalls the app. So in my opinion, it is just as good as not saving the image. This method is also more secure than saving it to external storage.

文档非常好(请参阅下面的进一步阅读),但有些部分有点棘手.这是对我有用的总结.

The documentation is pretty good (see the Further Reading below), but some parts are a little tricky. Here is a summary that worked for me.

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="androidx.core.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 替换为您的应用程序包名称.

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

<?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 从何处获取要共享的文件(在本例中使用缓存目录).

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

// 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 (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.myapp.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秒登录
发送“验证码”获取 | 15天全站免登陆