1

後,我使用此代碼複製使用documentFile.createFile()ContentResolver的不包含剛創建圖像中的即時查詢創建文件

private void newcopyFile(File fileInput, String outputParentPath, 
          String mimeType, String newFileName) { 

     DocumentFile documentFileGoal = DocumentFile.fromTreeUri(this, treeUri); 

     String[] parts = outputParentPath.split("\\/"); 
     for (int i = 3; i < parts.length; i++) { 
      if (documentFileGoal != null) { 
       documentFileGoal = documentFileGoal.findFile(parts[i]); 
      } 
     } 
     if (documentFileGoal == null) { 
      Toast.makeText(MainActivity.this, "Directory not found", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     DocumentFile documentFileNewFile = documentFileGoal.createFile(mimeType, newFileName); 

     InputStream inputStream = null; 
     OutputStream outputStream = null; 
     try { 
      outputStream = getContentResolver().openOutputStream(documentFileNewFile.getUri()); 
      inputStream = new FileInputStream(fileInput); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     try { 
      if (outputStream != null) { 
       byte[] buffer = new byte[1024]; 
       int read; 
       if (inputStream != null) { 
        while ((read = inputStream.read(buffer)) != -1) { 
         outputStream.write(buffer, 0, read); 
        } 
       } 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       inputStream = null; 
       outputStream.flush(); 
       outputStream.close(); 
       outputStream = null; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

的圖像,並且這是我的查詢ContentResolver創建圖像後,對立即刷新我的圖片庫,其查詢結果應包含新創建圖像的信息。

cursorPhotos = MainActivity.this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        projectionsImages, 
        null, 
        null, 
        MediaStore.Images.Media.DATE_TAKEN + " DESC" 
      ); 

但是,立即查詢無法找到新創建的圖像。 如果我稍後再次運行查詢,則新創建的圖像會出現在結果中。

它似乎爲新創建的圖像提供信息需要時間爲ContentResolver(如果ContentResolver負責它),因爲它將在後臺運行,而我運行立即查詢。

是否有任何方法或聽衆知道新創建的圖像何時由ContentResolver註冊?

+0

您是在複製異步任務內的圖像還是在主UI線程上? – Rahulrr2602

+0

@ Rahulrr2602是的,看到答案。 – Eftekhari

+0

非常感謝。如果我多次循環newcopyFile方法,那麼有時documentFileGoal爲null。 – Rahulrr2602

回答

1

你可以使用this或這是我如何實現一個監聽器(觀察者)到ContentResolver c使用ContentObserver來知道什麼時候運行查詢以獲取來自ContentResolver的新創建映像的適當時間。

首先創建ContentObserver類:

此類正如它的名字告訴我們,在觀察我們所期望的Uri任何內容的修改。

class MyObserver extends ContentObserver { 
    public MyObserver(android.os.Handler handler) { 
     super(handler); 
    } 

    @Override 
    public void onChange(boolean selfChange) { 
     this.onChange(selfChange, null); 
    } 

    @Override 
    public void onChange(boolean selfChange, Uri uri) { 
     //(SDK>=16) 
     // do s.th. 
     // depending on the handler you might be on the UI 
     // thread, so be cautious! 

     // This is my AsyncTask that queries ContentResolver which now 
     // is aware of newly created media file. 
     // You implement your own query here in whatever way you like 
     // This query will contain info for newly created image 
     asyncTaskGetPhotosVideos = new AsyncTaskGetPhotosVideos(); 
     asyncTaskGetPhotosVideos.execute(); 
    } 
} 

,並在您的複製方法結束時,你可以設置ContentObserverContentResolver上的特定URI。

getContentResolver().registerContentObserver(
     MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
     true, 
     myObserver); 

並且不要忘記取消註冊您的觀察者,否則您將面臨內存泄漏。我更喜歡在我的AsyncTask(onPostExecute)結尾。

getContentResolver().unregisterContentObserver(myObserver); 

你可以選擇對你的期望的URI ContentObserver通過你的整個生命週期中的應用程序,如果媒體得到了改變,刪除或插入來自外部或內部應用程式中得到通知。

對於這種方法,您可以在onResume()生命週期方法中註冊您的觀察者,並在onPause()方法中取消其註冊。

0

對不起,我錯貼代碼

當您將文件添加到Android的文件系統,這些文件不會被MedaScanner自動拾取。但他們通常應該是。

所以添加文件到內容提供商手動

所以使用這個代碼: -

Intent intent = 
     new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
intent.setData(Uri.fromFile(file)); 
sendBroadcast(intent); 

這樣做的更多的方法以供參考訪問網站: -

http://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/

+0

爲什麼我需要從媒體商店中刪除項目?我只需要一個'ContentResolver'的監聽器來完成刷新。 – Eftekhari

+0

文件已被添加,但'ContentProvider'沒有意識到它,我猜它是活動的(可能搜索所有媒體文件和其他東西),爲新創建的媒體提供信息。所以我只需要一個Observer('ContentObserver')來了解ContentResolver何時獲取新創建的圖像。看到我的答案和提供的鏈接。謝謝你的方式。 – Eftekhari

相關問題