0

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

4 回答 4

4

我用静态方法scanFile很好地工作

这是我的代码:

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

希望有帮助...

于 2011-06-14T13:13:32.230 回答
4

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

请参阅:带 Intent 的文件媒体扫描仪

于 2014-03-31T07:46:13.030 回答
2

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

于 2011-04-11T20:10:20.107 回答
0

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

于 2013-12-13T14:12:58.233 回答