【发布时间】:2014-07-15 03:59:32
【问题描述】:
我想用我的 ContentProvider 保存/加载二进制数据。为了节省,我写了这段代码:
long id = database.insert(TABLE_NAME, null, values);
Uri uri = ContentUris.withAppendedId(
LocationContentProvider.CONTENT_URI, id);
OutputStream outStream;
try {
Bitmap bmp = ...
if (bmp != null) {
outStream = cr.openOutputStream(uri);
ImageUtils.saveToStream(bmp, outStream,
Bitmap.CompressFormat.PNG);
outStream.close();
bmp.recycle();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d(TAG, "Could not save logo to " + uri.toString());
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "Could not save logo to " + uri.toString());
}
当然,最初该文件并不存在。所以我得到了 FileNotFoundException。我的问题是,如果 ContentProvider 尚不存在,如何强制创建文件?我需要实现 ContentProvider.openFile 吗?
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
return super.openFile(uri, mode);
}
【问题讨论】:
标签: android android-contentprovider