动态添加图片到图库小部件

eye*_*ate 7 android

有没有一种在运行时将新图像资源(从SD卡)添加到图库窗口小部件的好方法?

Pet*_*ego 25

"新形象资源"?

图像资源是.apk应用程序包中/ res/drawable文件夹的一部分.您无法在运行时添加"新"图像资源.

你有没有其他一些用例?

海报解释后编辑:

您必须将媒体文件添加到Media Store才能被gallery小部件看到.使用MediaScanner.我在我的代码中使用这个方便的包装器:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后实例化MediaScannerWrapper并启动它scan().你可以调整它来处理多个文件.提示:传递文件路径列表,然后循环mConnection.scanFile.

  • 不幸的是,似乎没有更新使用该信息的应用...例如,下载mp3并运行此代码将扫描它,但不会添加到默认媒体播放器.要做到这一点,你需要context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 如下所述:http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android (2认同)