android: how to add an image to an album

ぐ巨炮叔叔 提交于 2019-12-04 17:22:24

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);

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();
    }
}

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?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!