I've been trying and trying to get this to work. I've gone through many examples with no luck. The problem is that the image is not attached when I try to share it, for example, using an email client. I managed to get this to work when using external storage but internal storage suits my needs better.
I click a button and then the image is saved to internal storage and right after that it is shared but there's no image.
This is from the Activity
class:
int width = size.x;
int height = size.y;
Bitmap shareBmp = Bitmap.createBitmap(screenBmp, 0, 0, width, height);
ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
File directory = wrapper.getDir("images", Context.MODE_PRIVATE);
File filePath = new File(directory, "share.png");
FileOutputStream fos;
try
{
fos = new FileOutputStream(filePath);
shareBmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
}
catch (Exception aE){}
Uri uri = Uri.parse("content://com.example.Test/share.png");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share Image"));
Here's the ImageProvider
class:
public class ImageProvider extends ContentProvider
{
@Override
public ParcelFileDescriptor openFile(Uri aUri, String aMode) throwsFileNotFoundException
{
File file = new File(getContext().getFilesDir(), aUri.getPath());
if (file.exists())
{
return (ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
}
throw new FileNotFoundException(aUri.getPath());
}
@Override
public boolean onCreate()
{
return false;
}
@Override
public int delete(Uri aUri, String aSelection, String[] aSelectionArgs)
{
return 0;
}
@Override
public String getType(Uri aUri)
{
return null;
}
@Override
public Uri insert(Uri aUri, ContentValues aValues)
{
return null;
}
@Override
public Cursor query(Uri aUri, String[] aProjection, String aSelection, String[] aSelectionArgs, String aSortOrder)
{
return null;
}
@Override
public int update(Uri aUri, ContentValues aValues, String aSelection, String[] aSelectionArgs)
{
return 0;
}
}
This is from the manifest:
<provider
android:name=".ImageProvider"
android:authorities="com.example.Test"
android:exported="true"/>