I am trying to store an image, which is located in my project's drawable folder to the gallery of my Samsung Android tablet. I have been able to successfully achieve this for an HTC phone using the exact same code; however, it appears when attempting this for a Samsung tablet, the URI I am using to insert the image is appearing not to exist. The error appears to occur when I attempt to open the ouput stream via the line...
OutputStream imageOut = getContentResolver().openOutputStream(url);
As mentioned before, this exact code worked for an HTC android phone, which leads me to believe something is different in the file system of the two devices as to where to store the image. The full code I am using (which is mainly copied from the MediaStore class) is found below.
Drawable drawable = getResources().getDrawable(R.drawable.sample);
Bitmap bitmapImage = ((BitmapDrawable)drawable).getBitmap();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "demo_image");
values.put(MediaStore.Images.Media.DESCRIPTION, "sample image");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri url = null;
try {
url = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
if (bitmapImage != null) {
OutputStream imageOut = getContentResolver().openOutputStream(url);
try {
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
} finally {
imageOut.close();
}
long id = ContentUris.parseId(url);
// Wait until MINI_KIND thumbnail is generated.
Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
// This is for backward compatibility.
// Bitmap microThumb = MediaStore.Images.Thumbnail(getContentResolver(), miniThumb, id, 50F, 50F,
// MediaStore.Images.Thumbnails.MICRO_KIND);
} else {
//Log.e(TAG, "Failed to create thumbnail, removing original");
getContentResolver().delete(url, null, null);
url = null;
}
} catch (Exception e) {
//Log.e(TAG, "Failed to insert image", e);
if (url != null) {
getContentResolver().delete(url, null, null);
url = null;
}
}