Dynamically Add Pictures To Gallery Widget
Solution 1:
"new image resources"?
Image resources are a part of /res/drawable folder inside your .apk application package. You can not add "new" image resources during runtime.
Is there some other use case you had in mind?
Edited after posters explanation:
You have to add media files to Media Store in order to be seen by gallery widget. Use MediaScanner. I use this convenient wrapper in my code:
publicclassMediaScannerWrapperimplementsMediaScannerConnection.MediaScannerConnectionClient {
privateMediaScannerConnection mConnection;
privateString mPath;
privateString mMimeType;
// filePath - where to scan; // mime type of media to scan i.e. "image/jpeg". // use "*/*" for any mediapublicMediaScannerWrapper(Context ctx, String filePath, String mime){
mPath = filePath;
mMimeType = mime;
mConnection = newMediaScannerConnection(ctx, this);
}
// do the scanningpublicvoidscan() {
mConnection.connect();
}
// start the scan when scanner is readypublicvoidonMediaScannerConnected() {
mConnection.scanFile(mPath, mMimeType);
Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
}
publicvoidonScanCompleted(String path, Uri uri) {
// when scan is completes, update media file tags
}
}
Then instantiate MediaScannerWrapper
and start it with scan()
. You could tweak it to handle more than one file at the time. Hint: pass List of File paths, and then loop around mConnection.scanFile
.
Solution 2:
Send broadcast to MediaStore Content Provider
when you add a file
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));
Working for devices before KitKat
sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Also have a look at this
Working in Lolipop and should also solve kitkat issues.
ContentValues values=new ContentValues();
values.put(MediaStore.Images.Media.DATA,"file path");
values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Add Permission.
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Post a Comment for "Dynamically Add Pictures To Gallery Widget"