动态地将图片添加到图库部件

动态地将图片添加到图库部件

本文介绍了动态地将图片添加到图库部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有新的图像资源(从SD卡)添​​加到图库部件在运行时的好办法?

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

推荐答案

新形象资源?

图片资源是/ RES /你的apk应用包内绘制文件夹中的一部分。运行过程中不能添加的新形象资源。

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?

之后海报解释编辑:

您需要的媒体文件添加到媒体商店,以便通过的画廊插件可以看出。使用MediaScanner。我在code使用这个方便的包装:

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
    }
}

然后实例 MediaScannerWrapper 启动扫描()。你可以调整它来处理多个文件的时候。提示:通过文件路径列表,然后循环回路 mConnection.scanFile

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.

这篇关于动态地将图片添加到图库部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:19