【问题标题】:Share Image File from cache directory Via Intent~android通过 Intent~android 从缓存目录共享图像文件
【发布时间】:2013-10-06 10:30:10
【问题描述】:

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

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 从设备中提取它并在系统上查看。

【问题讨论】:

标签: android android-intent share


【解决方案1】:

我按照以下步骤共享缓存的图像。

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

public static File copyImage(String sourcePath, String targetPath){
试试{
InputStream in = new FileInputStream(sourcePath);
OutputStream out = new FileOutputStream(targetPath);
字节[] buf = 新字节[1024];
国际化;
而 ((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);

【讨论】:

  • 是的,好的。但是您仍然会在缓存之外获得文件副本。那不是我当时想要做的。我终于从 start 本身在sdcard 目录之外创建了文件。
【解决方案2】:

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

}

文档

【讨论】:

    【解决方案3】:

    这是什么原因?

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

    他们都无法解决问题

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多