Android的分享意图位图 - 这可能不救它之前共享?位图、能不、这可、意图

2023-09-13 01:56:53 作者:浮光幻影流年

我尝试导出使用份额的意图我的应用程序的位图,不保存文件的时间位置。我所发现的例子两步 1)保存到SD卡,并创建乌里该文件 2)启动的意图与此URI

是否有可能使其无需WRITE_EXTERNAL_STORAG​​E权限,保存文件[事后删除它]?如何解决设备没有ExternalStorage?

解决方案

我有同样的问题。我不希望有要求的读取和写入外部存储权限。此外,有时也有问题,当手机没有SD卡或卡获得卸载。

以下方法使用一个ContentProvider所谓的FileProvider.从技术上讲,你仍然保存位图(在内部存储)之前共享,但你并不需要申请任何权限。此外,每次大家分享位图图像文件被覆盖。而且,由于它是在内部缓存中,它会被当用户卸载应用程序删除。所以,在我看来,它只是不如不保存图像。这种方法也比它保存到外部存储更安全。

该文档是pretty的好(请参阅下面的延伸阅读),但有些部分是有点棘手。这里是为我工作的一个总结。

设置的FileProvider在清单

 <舱单>
    ...
    <应用>
        ...
        <供应商
            机器人:名称=android.support.v4.content.FileProvider
            机器人:当局=com.example.myapp.fileprovider
            机器人:grantUriPermissions =真
            机器人:出口=假>
            &所述;元数据
                机器人:名称=android.support.FILE_PROVIDER_PATHS
                机器人:资源=@ XML /文件路径/>
        < /供应商>
        ...
    < /用途>
< /舱单>
 

替换 com.example.myapp 您的应用程序包的名字。

创建RES / XML / filepaths.xml

 < XML版本=1.0编码=UTF-8&GT?;
<路径的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <缓存路径名=shared_images路径=图像//>
< /路径>
 

这告诉FileProvider何处获取文件共享(使用在这种情况下,高速缓存目录中)。

将图像保存到内部存储器

  //保存位图缓存目录
尝试 {

    文件cachePath =新的文件(context.getCacheDir(),图像);
    cachePath.mkdirs(); //不要忘了做目录
    的FileOutputStream流=新的FileOutputStream(cachePath +/image.png); //每一次改写这个图片
    bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,流);
    stream.close();

}赶上(FileNotFoundException异常E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
}
 
记录学习Android基础的心得03 活动activity和意图intent

分享图片

 文件的ImagePath =新的文件(context.getCacheDir(),图像);
文件NEWFILE =新的文件(的ImagePath,image.png);
乌里contentUri = FileProvider.getUriForFile(背景下,com.example.app.fileprovider,NEWFILE);

如果(contentUri!= NULL){

    意图shareIntent =新意图();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //用于接收应用程序读取该文件的临时许可
    shareIntent.setDataAndType(contentUri,getContentResolver()的getType(contentUri));
    shareIntent.putExtra(Intent.EXTRA_STREAM,contentUri);
    startActivity(Intent.createChooser(shareIntent,选择一个应用程序));

}
 

进一步阅读

FileProvider 存储选项 - 内部存储 共享文件 保存文件

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

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?

解决方案

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.

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.

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

FileProvider Storage Options - Internal Storage Sharing Files Saving Files
 
精彩推荐
图片推荐