通过Intent分享缓存目录中的图像文件~android

24

我正在尝试分享缓存目录中的图像文件,我已经有了完整路径,但是无法将文件作为附件发送,以下是代码:

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分享也没有在帖子中显示任何图片。

这是什么原因?

我已经查看了以下Stack Overflow链接如何在Android中使用共享意图共享图像和其他许多链接,但没有一个能够解决问题。

P.S.:

1. 目标是对屏幕进行截图,将其保存在缓存目录中,并从那里在线共享。

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


2
缓存目录是您的应用程序的私有目录。共享应用无法访问该文件。 - Sherif elKhatib
1
阅读http://developer.android.com/guide/topics/data/data-storage.html#filesExternal, 保存应共享的文件。 - Emanuel Moecklin
1
如果您不想使用外部存储器,您需要创建一个ContentProvider来访问文件,无论您将它们放在哪里(即使在缓存目录中也可以)。 - Emanuel Moecklin
请查看stackoverflow.com上的https://dev59.com/RW7Xa4cB1Zd3GeqPp3EW?rq=1链接,了解有关使用意图共享图像的正确方法。该链接还指向http://stephendnicholas.com/archives/974以获取内容提供程序,但根据stackoverflow.com的帖子,这似乎无法正常工作! - Akhil Jain
找到了一个关于Android开发的博客分享意图,网址在这里http://android-developers.blogspot.in/2012/02/share-with-intents.html,解释得非常详细。 - Akhil Jain
你可以将文件复制到外部缓存并以此方式共享它。 - Aggressor
3个回答

67

我遵循了@CommonsWare的建议,并使用了FileProvider。假设您的图像已经在内部缓存目录中作为cache/images/image.png,则可以按照以下步骤进行操作。这些步骤大多是文档的概括。

在清单文件中设置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"));

}

文档


14

这是什么原因?

如前所述,其他应用程序无法访问您的应用程序的内部存储。

它们中没有一个能够解决这个问题

请随意在 StackOverflow 上发布新问题,在其中完整而准确地解释您尝试过哪些具体解决方案以及遇到了哪些具体问题。

但是根据 SO 帖子,似乎并不起作用!

请随意在 StackOverflow 上发布新问题,在其中完整而准确地解释“似乎并不起作用”是什么意思。

或者,使用FileProvider,该提供了此功能,除了在清单中添加条目外,无需编写代码。

或者,将您的图像存储在外部存储器上,例如getExternalCacheDir()


0

我通过以下步骤分享缓存图像。

1.将缓存的图像复制到目标路径。

public static File copyImage(String sourcePath, String targetPath){
        try {
               InputStream in = new FileInputStream(sourcePath);
               OutputStream out = new FileOutputStream(targetPath);
               byte[] buf = new byte[1024];
               int len;
               while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);
               }
               in.close();
               out.close();
               return new File(targetPath);
          } catch (IOException e) {
               e.printStackTrace();
               return null;
          }
  }

2.获取复制文件的Uri。

Uri uri = Uri.fromFile(target);

3.使用意图分享图像。

    File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
    if(!dir.exists()){
        dir.mkdirs();
    }
    File f = new File(dir,"temporary_file.jpg");
    File target = copyImage(url,f.getAbsolutePath());
    Uri uri = Uri.fromFile(target);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM,uri );
    context.startActivity(intent);

是的,没错。但你最终还是会在缓存之外拥有文件的副本。那不是我当时想要做的。最终我从一开始就在sdcard目录之外创建了文件。 - Akhil Jain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接