I want to save/load binary data with my ContentProvider. For saving I wrote this code:
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());
}
But of course, initially the file does not exist. So I get the FileNotFoundException. My question is, how do I force the ContentProvider to create the file if not yet existent? And do I need to implement ContentProvider.openFile?
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
return super.openFile(uri, mode);
}