Exporting image through ContentProvider
I´m trying to export an image from my application to other applications like twitter, facebook or other apps accepting it images. I'm doing the following:
private void exportImage()
{
storeBitmapOnDisk(this.bitmap);
Intent i = new Intent(Intent.ACTION_SEND);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
File imgFile = context.getFileStreamPath("export.png");
Uri uri = Uri.fromFile(imgFile);
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("image/png");
i.setComponent(componentName);
context.startActivity(i);
}
private void storeBitmapOnDisk(Bitmap bitmap)
{
try
{
FileOutputStream outStream = context.openFileOutput("export.png",
Context.MODE_WORLD_READABLE);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, outStream);
outStream.close();
}
catch (IOException e)
{
Log.d(TAG, e.getMessage());
}
}
This is not working because I'm writing internal storage with is not accessible by other applications. As I don't want to use external storage (SD card) I think what I need is a ContentProvider but all examples I saw is about custom ContentPovider are using a SQlite database to store the data. I can't figure out how I could store a bitmap in internal storage and make it available through a ContentProvider to other applications without storing my bitmap in a database. MatrixCursor seems not to be adapted too...
SOLUTION I create a custom Content Provider and set the path of the file in the the content provider as the Uri EXTRA_STREAM extra parameter of the intent:
First my provider class where I only needed to override openFile which is not clear in the doc...
public class ExportContentProvider extends ContentProvider
{
public static Uri CONTENT_URI = Uri
.parse("content://com.path.to.my.provider");
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
return 0;
}
@Override
public String getType(Uri uri)
{
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
return null;
}
@Override
public boolean onCreate()
{
return false;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
{
int imode = 0;
if (mode.contains("w"))
imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
if (mode.contains("r"))
imode |= ParcelFileDescriptor.MODE_READ_ONLY;
if (mode.contains("+"))
imode |= ParcelFileDescriptor.MODE_APPEND;
try
{
return ParcelFileDescriptor.open(new File(uri.getEncodedPath()), imode);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
@Override
public Curso开发者_运维百科r query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
return 0;
}
}
And then my export function:
private void exportImage()
{
storeBitmapOnDisk();
Intent i = new Intent(Intent.ACTION_SEND);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.putExtra(Intent.EXTRA_STREAM, Uri.withAppendedPath(
ExportContentProvider.CONTENT_URI, context.getFileStreamPath(
"export.png").getAbsolutePath()));
i.setType("image/png");
i.setComponent(componentName);
if (exportFileObserver == null)
{
this.bitmapPath = context.getFileStreamPath("export.png")
.getAbsolutePath();
exportFileObserver = new ExportFileObserver(this.bitmapPath);
exportFileObserver.startWatching();
}
listener.launchExportActivity(Dms.ACT_IMAGE_EXPORT, i);
}
Nevertheless there are 2 problems remaining: - When trying to delete my temporary export.png file on my internal memory using result of the activity I received the onResult when the activity is launch... - It s not working for mail app in my 2.3 emulator. I'm getting "File to large" message...
Somenone tested that ?
精彩评论