首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android如何从资产文件夹共享图像?

android如何从资产文件夹共享图像?
EN

Stack Overflow用户
提问于 2018-03-21 02:08:59
回答 2查看 0关注 0票数 0

我试图从我的资产文件夹共享图片。我的代码是:

代码语言:javascript
复制
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/myImage.jpg"));
startActivity(Intent.createChooser(share, "Share This Image"));
EN

Stack Overflow用户

发布于 2018-03-21 11:34:50

这样的方法可行:

代码语言:javascript
复制
<provider android:name="yourclass.that.extendsContentProvider"                android:authorities="com.yourdomain.whatever"/>

内容提供者类具有以下内容:

代码语言:javascript
复制
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    AssetManager am = getContext().getAssets();
    String file_name = uri.getLastPathSegment();
    if(file_name == null) 
        throw new FileNotFoundException();
    AssetFileDescriptor afd = null;
    try {
        afd = am.openFd(file_name);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return afd;//super.openAssetFile(uri, mode);
}

调用代码执行此操作:

代码语言:javascript
复制
Uri theUri = Uri.parse("content://com.yourdomain.whatever/someFileInAssetsFolder");
Intent theIntent = new Intent(Intent.ACTION_SEND);
theIntent.setType("image/*");
theIntent.putExtra(Intent.EXTRA_STREAM,theUri);
theIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject for message");                        
theIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Body for message");
startActivity(theIntent);
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档