android 使用媒体扫描仪扫描 SD 卡

Posted

技术标签:

【中文标题】android 使用媒体扫描仪扫描 SD 卡【英文标题】:android using media scanner for SD card 【发布时间】:2011-04-11 17:35:04 【问题描述】:

大家好 我试图将图像保存到 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);
        
    

【问题讨论】:

“文件”未声明。你忘了 import android.media.MediaScannerConnection 吗? 我导入了 mediaScannerConnection 仍然给我同样的问题....??!!唯一显示的修复是“修复项目设置..” 【参考方案1】:

我用静态方法 scanFile 很好地完成了这个工作

这是我的代码:

String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + fileName;    
MediaScannerConnection.scanFile(this, new String[]  filePath , null, null);

希望对您有所帮助...

【讨论】:

【参考方案2】:

或者可以通过 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);

见:Files Media Scanner with Intent

【讨论】:

【参考方案3】:

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()。

【讨论】:

【参考方案4】:

您在代码中使用File file;,但您没有此对象相反,您在代码的其他部分使用File outputFile。所以你需要在你的MediaScannerConnection.scanFile(...) 中使用outputFile.getAbsolutePath()

【讨论】:

以上是关于android 使用媒体扫描仪扫描 SD 卡的主要内容,如果未能解决你的问题,请参考以下文章

android如何实现扫描sd卡图片,并统计图片个数

如何在android中运行媒体扫描仪

用于 Android Q 上的辅助存储的媒体扫描仪

自己写的android camera应用,拍完的照片保存在sd卡中,但是不能在gallery中看不到,重启手机后才能看到

Android4.4中不能发送SD卡就绪广播

如何扫描出Android系统媒体库中视频文件