2011-04-11 52 views
0

喜所有 我嘗試將圖像保存到SD卡,並利用媒體掃描儀掃描新的保存文件,以便它可以立即的具有1M問題的Android使用的媒體掃描儀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); 
     } 
    } 
+0

「文件」 未聲明。 你忘了導入android.media.MediaScannerConnection? – 2011-04-11 17:48:58

+0

我導入了mediaScannerConnection並仍然給我同樣的問題.... ?? !!並且顯示的唯一修復方法是「修復項目設置..」 – moe 2011-04-11 18:57:42

回答

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

4

我得到這個與靜態方法工作很好scanFile

這裏是我的代碼:

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

希望幫助...

0

你在你的代碼中使用File file;,但你沒有這個對象而是在代碼的其他部分使用File outputFile。所以你需要在你的MediaScannerConnection.scanFile(...)中使用outputFile.getAbsolutePath()

4

,或者可以通過意向使用:

//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

+0

雖然此鏈接可能回答此問題,但最好在此處包含答案的基本部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – sh0ber 2014-03-31 08:05:05

+0

好的,謝謝,代碼已更新 – hungtdo 2014-03-31 08:15:49