从缓存目录中共享图像文件通过Intent~android

Akh*_*ain 16 android share android-intent

我试图在缓存目录中共享图像文件,我有完整的路径,但无法在附件中发送文件,代码是

File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath()); 
*/


Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));
Run Code Online (Sandbox Code Playgroud)

上面注释的代码不起作用

发送邮件显示附件,但没有收到结束没有附件,Facebook共享也显示邮件中没有图像

这是什么原因?

我已经看到以下SO链接如何使用共享图像使用共享意图共享图像在Android和许多其他人,他们都没有能够解决这个问题

PS;

1.目的是将屏幕截图保存在缓存目录中并从那里在线共享

2.我确实有文件,我可以通过DDMS从设备中取出它并在系统上查看.

Sur*_*gch 54

我按照@ CommonsWare的建议使用了FileProvider.假设您的图像已经在内部缓存目录中cache/images/image.png,那么您可以使用以下步骤.这些主要是文档的合并.

在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在哪里获取要共享的文件(在这种情况下使用缓存目录).

分享图片

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)

文档


Com*_*are 9

这是什么原因?

如上所述,其他应用无法访问您应用的内部存储空间.

他们都没有能够解决这个问题

请随意打开一个新的StackOverflow问题,在那里您可以完整而准确地解释您尝试过的具体解决方案以及遇到的具体问题.

但这似乎并没有像SO帖子那样工作!

随意打开一个新的StackOverflow问题,在那里你完整而准确地解释"那似乎不起作用"的含义.

或者,使用FileProvider,提供此功能,除了清单中的条目之外不需要任何代码.

或者,将图像存储在外部存储设备上,例如getExternalCacheDir().