问题
Can anyone share code (or point me to Android sample code) to help me add images to an album in the Media Store (Gallery).
In my app I download images from our server, and also take new images using the camera (via Intent).
I would like to organize those images in an app-specific album, similar to what the Facebook (and other apps) app does, keeping related images all neatly organized.
I looked into this a while ago, following the Media Store API docs, and it didn't work for me....so need some help.
Thanks
回答1:
you can use:
MediaStore.Images.Media.insertImage(ContentResolver cr, Bitmap source, String title, String description);
and I'm sure that somewhere here http://developer.android.com/guide/ in some of the sub-menus shows a command asking the MediaStore to scan a determined folder, I just can't find it now.
edit: found it:
MediaScannerConnection.scanFile(Context context, String[] path, null, null);
回答2:
This is a method used to store an image in the public Picture folder, with a custom app folder in it.
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
//Create Path to save Image
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
path.mkdirs();
File imageFile = new File(path, imgName+".png"); // Imagename.png
FileOutputStream out = new FileOutputStream(imageFile);
try{
bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch(Exception e) {
throw new IOException();
}
}
回答3:
Here is the code I ended up using to do this:
public static Uri addToTouchActiveAlbum( Context context, String title, String filePath ) {
ContentValues values = new ContentValues();
values.put( Media.TITLE, title );
values.put( Images.Media.DATE_TAKEN, System.currentTimeMillis() );
values.put( Images.Media.BUCKET_ID, filePath.hashCode() );
values.put( Images.Media.BUCKET_DISPLAY_NAME, Constants.TA_PHOTO_ALBUM_NAME );
values.put( Images.Media.MIME_TYPE, "image/jpeg" );
values.put( Media.DESCRIPTION, context.getResources().getString( R.string.product_image_description ) );
values.put( MediaStore.MediaColumns.DATA, filePath );
Uri uri = context.getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values );
return uri;
}
It works for the Images I have in "getExternalStorage()" (/storage/sdcard0)
I'd also like to add images I have in a different folder (a cacheDir I create under the folder returned by Context.getExternalCacheDir()). Problem: I don't know their mime-type (does that matter?), the folder is a different one in a different location with a different name - and I can't figure out how to add to an "album" per se.....as the album name seems to come from the folder name....
Or to put it another way:
values.put( Images.Media.BUCKET_DISPLAY_NAME, Constants.TA_PHOTO_ALBUM_NAME );
doesn't seem to have any effect?
来源:https://stackoverflow.com/questions/11983654/android-how-to-add-an-image-to-an-album