我正在尝试将sdcard上数据目录中的文件添加到mediastore内容提供程序。但是,我担心我手上可能会遇到Java教育问题。 MediaScannerConnectionClient据说是MediaScannerConnection的嵌套类,但是此语法无法编译。
这是MediaScannerConnection API的链接:http://developer.android.com/reference/android/media/MediaScannerConnection.html
参考扫描仪的合适方法是什么?
谢谢!
final String filename = (new File(img.uri.toString())).getAbsolutePath().substring(6);
final MediaScannerConnection scanner = new MediaScannerConnection(this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
MediaScannerConnection.this.scanFile(filename, null /*mimeType*/);
}
public void onScanCompleted(String path, Uri uri) {
MediaScannerConnection.this.disconnect();
}
});
scanner.connect();
最佳答案
我终于在一个例子中找到了答案。
... / android-8 / ApiDemos / src / com / example / android / apis / content / ExternalStorage.java
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { filename }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});