Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically add pictures to gallery widget

Tags:

android

Is there a good way to add new image resources(from SD card) to a gallery widget at runtime?

like image 974
eyecreate Avatar asked Nov 11 '10 18:11

eyecreate


People also ask

How do I use the image gallery widget?

The Image Gallery widget allows you to easily add and style complex and beautiful image galleries on your page. Type: Select type of gallery, choosing from Single or Multiple. Multiple allows you to have a filterable portfolio-style gallery of images If Multiple is chosen, enter a title into the field or use Dynamic Tags *

How do I add images to my gallery?

Click Add Image button to select images to display. Once selected, click Create a New Gallery button and then click the Insert Gallery button. Image Size: Choose the size of the image, from thumbnail to full Link to: Link images to their respective Media Files, Attachment Pages, or None (Media File must be selected if Lightbox is enabled)

How to add a gallery to a WordPress post?

Create the Gallery meta field and hit the “ Update ” button. Then, open the post, scroll it to the meta fields and add pictures to the Gallery meta field. After that, click the “ Update ” button. Open the post’s listing template with Elementor editor. To showcase the post gallery, find the Dynamic Field widget in the panel and drop it to the page.

How can I customize the default image galleries in WordPress?

Showcase image galleries for default posts using meta fields of the gallery type from the meta box created with JetEngine. You will be able to customize the appearance of the grid and slider galleries according to your needs in the Dynamic Field widget.


2 Answers

"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:

public class MediaScannerWrapper implements  
MediaScannerConnection.MediaScannerConnectionClient {
    private MediaScannerConnection mConnection;
    private String mPath;
    private String mMimeType;

    // filePath - where to scan; 
    // mime type of media to scan i.e. "image/jpeg". 
    // use "*/*" for any media
    public MediaScannerWrapper(Context ctx, String filePath, String mime){
        mPath = filePath;
        mMimeType = mime;
        mConnection = new MediaScannerConnection(ctx, this);
    }

    // do the scanning
    public void scan() {
        mConnection.connect();
    }

    // start the scan when scanner is ready
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, mMimeType);
        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
    }

    public void onScanCompleted(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.

like image 67
Peter Knego Avatar answered Oct 12 '22 15:10

Peter Knego


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(new Intent(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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 29
Zar E Ahmer Avatar answered Oct 12 '22 14:10

Zar E Ahmer