android 内部共享存储,android – 共享存储在内部存储器中的图像

我想这里的问题是你正在尝试打开一个保存在私有内存空间中的文件库(getCacheDir返回一个相对于你的应用程序的路径,只有你的应用程序可以访问那个内存路径)

如果您无法保存在外部存储器中,则可以尝试保存在公共路径中(但是这样,您的媒体文件可以被每个应用程序操纵,如果您卸载应用程序,它不会清除您保存在那里的生成的媒体)

如果要使用专用内部存储器,可以编写ContentProvider

我编辑发布我用来完成我所说的内容提供商.

这是我的内容提供商(我刚刚发布了您需要的相关部分):

public class MediaContentProvider extends ContentProvider {

private static final String TAG = "MediaContentProvider";

// name for the provider class

public static final String AUTHORITY = "com.way.srl.HandyWay.contentproviders.media";

private MediaData _mediaData;

// UriMatcher used to match against incoming requests

private UriMatcher _uriMatcher;

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

// TODO Auto-generated method stub

return 0;

}

@Override

public String getType(Uri uri) {

// TODO Auto-generated method stub

return null;

}

@Override

public Uri insert(Uri uri, ContentValues values) {

// TODO Auto-generated method stub

return null;

}

@Override

public boolean onCreate() {

uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

// Add a URI to the matcher which will match against the form

// 'content://com.stephendnicholas.gmailattach.provider/*'

// and return 1 in the case that the incoming Uri matches this pattern

_uriMatcher.addURI(AUTHORITY, "*", 1);

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

// TODO Auto-generated method stub

return null;

}

@Override

public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

// TODO Auto-generated method stub

return 0;

}

@Override

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {

Log.v(TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment());

// Check incoming Uri against the matcher

switch (_uriMatcher.match(uri)) {

// If it returns 1 - then it matches the Uri defined in onCreate

case 1:

// The desired file name is specified by the last segment of the

// path

// E.g.

// 'content://com.stephendnicholas.gmailattach.provider/Test.txt'

// Take this and build the path to the file

// String fileLocation = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();

Integer mediaID = Integer.valueOf(uri.getLastPathSegment());

if (_mediaData == null) {

_mediaData = new MediaData();

}

Media m = _mediaData.get(mediaID);

// Create & return a ParcelFileDescriptor pointing to the file

// Note: I don't care what mode they ask for - they're only getting

// read only

ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(m.filePath), ParcelFileDescriptor.MODE_READ_ONLY);

return pfd;

// Otherwise unrecognised Uri

default:

Log.v(TAG, "Unsupported uri: '" + uri + "'.");

throw new FileNotFoundException("Unsupported uri: " + uri.toString());

}

}

那么你需要在清单中提供对你的contentprovider的引用,就我的情况而言

android:name=".contentproviders.MediaContentProvider"

android:authorities="com.way.srl.HandyWay.contentproviders.media" >

然后像这样使用它

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.parse("content://" + MediaContentProvider.AUTHORITY + "/" + m.id), "image/jpg");

在我的情况下,m是一个存储指向sqlite数据库的id的实体,我使用一个获取数据的类来再次填充对象(使用_mediaData),您只需更改代码以满足您的需求

这样我在我的应用程序中解决了你的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值