[英]How to invoke Android media scanner for vendor provided external SD card, not the one obtained via getExternalStorage()
[英]android using media scanner for SD card
大家好,我试图将图像保存到SD卡,并且在使用媒体扫描仪扫描新保存的文件时遇到问题,以便立即可用
错误在以下几行中
new String[] { file.toString() }, null, // error: file cannot be resolved
new MediaScannerConnection.OnScanCompletedListener() {
// error: MediaScannerConnection.OnScanCompletedListener cannot be resolved to a type
这是我的代码:
public void saveToSDCard(Bitmap bitmap, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.v(TAG, "SD Card is available for read and write "
+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile(bitmap, name);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.v(TAG, "SD Card is available for read "
+ mExternalStorageAvailable);
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.v(TAG, "Please insert a SD Card to save your Ad "
+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}
private void saveFile(Bitmap bitmap, String name) {
String filename = name;
ContentValues values = new ContentValues();
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources().getString(
R.string.directory));
sdImageMainDirectory.mkdirs();
File outputFile = new File(sdImageMainDirectory, filename);
values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.DATE_ADDED, System
.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
Uri uri = this.getContentResolver().insert(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
try {
OutputStream outStream = this.getContentResolver()
.openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);
outStream.flush();
outStream.close();
// this is where im having the problem
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.v("ExternalStorage", "Scanned " + path + ":");
Log.v("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.v("ExternalStorage", "Error writing " + file, e);
}
}
或者可以通过Intent使用:
//Sends a broadcast to have the media scanner scan a file
private void scanMedia(String path) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
}
请参阅: 具有意图的文件媒体扫描仪
我通过静态方法scanFile很好地工作了
这是我的代码:
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + fileName;
MediaScannerConnection.scanFile(this, new String[] { filePath }, null, null);
希望有帮助...
MediaScannerConnection.OnScanCompletedListener仅在API级别8起可用。与您尝试使用的scanFile()变体相同。 检查清单中的minSdkVersion(和targetSdkVersion)。
如果您需要使应用程序与Froyo之前的Android版本兼容,则应使用scanFile(String path,String mimeType)变体。 它不是静态的,因此您需要实例化和缓存连接,例如在onResume()中:
scanner = new MediaScannerConnection(this, null);
scanner.connect();
然后,每当保存文件时:
if (scanner.isConnected())
scanner.scanFile(someFile, null);
不要忘记在onPause()中调用scanner.disconnect()。
您使用File file;
在您的代码中,但是您没有此对象,而是在代码的其他部分使用File outputFile
。 因此,您需要在MediaScannerConnection.scanFile(...)
使用outputFile.getAbsolutePath()
MediaScannerConnection.scanFile(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.