gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:26:43 24 4
gpt4 key购买 nike

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

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

上面注释的代码不起作用

发送邮件显示附件,但接收端没有附件,facebook分享也没有显示帖子中的图像

这是什么原因??

我已经看过以下 SO 链接 how-to-use-share-image-using-sharing-intent-to-share-images-in-android和许多其他人,他们都无法解决这个问题

附:

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

2.是的,我有文件,我可以通过 DDMS 从设备中提取它并在系统上查看。

最佳答案

我遵循@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>

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>

这告诉 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"));

}

文档

关于android - 通过 Intent~android 从缓存目录共享图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19096275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com