22

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

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 Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android和许多其他的,他们都无法解决这个问题

附言;

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

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

4

3 回答 3

62

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

}

文档

于 2015-05-11T16:11:29.060 回答
13

这是什么原因?

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

他们都无法解决问题

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

但这似乎并没有按照 SO 帖子工作!!!

随意打开一个新的 StackOverflow 问题,您可以在其中完整而准确地解释“这似乎不起作用”的含义。

或者,useFileProvider,它提供了此功能,除了清单中的条目之外不需要任何代码。

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

于 2013-09-30T14:31:03.363 回答
0

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

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

public static File copyImage(String sourcePath, String targetPath){
        try {
               InputStream in = new FileInputStream(sourcePath);
               OutputStream out = new FileOutputStream(targetPath);
               字节[] buf = 新字节[1024];
               国际化;
               while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);
               }
               in.close();
               out.close();
               返回新文件(目标路径);
          } 捕捉(IOException e){
               e.printStackTrace();
               返回空值;
          }
  }

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);
于 2015-08-17T08:13:37.290 回答