2014-10-31 60 views
0

我遇到MediaScanner問題。我正在使用本機相機應用拍攝照片,並且我想將ImageView設置爲此圖像。它在我從圖庫中選擇圖片時起作用,但我無法讓應用程序等待MediaScanner完成對我新拍攝圖像的掃描。這是我的代碼:等待MediaScanner掃描文件

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_IMAGE_CAPTURE) { 
      final boolean isCamera; 
      if (data == null) { 
       isCamera = true; 
      } else { 
       final String action = data.getAction(); 
       if (action == null) { 
        isCamera = false; 
       } else { 
        isCamera = action 
          .equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       } 
      } 

      Uri selectedImageUri; 
      if (isCamera) { 
       selectedImageUri = outputFileUri; 
      } else { 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = this.getContentResolver().query(
         selectedImage, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       selectedImageUri = Uri.parse(cursor.getString(columnIndex)); 
       cursor.close(); 
      } 
      // The app is not waiting for these lines, 
      // I have also tried them alone each of them. 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
        outputFileUri)); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED, 
        outputFileUri)); 

      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(selectedImageUri.toString(), options); 
      options.inSampleSize = calculateInSampleSize(options, 
        dpToPx(100), dpToPx(100)); 

      // Decode bitmap with inSampleSize set 
      options.inJustDecodeBounds = false; 
      Bitmap bMapRotate = BitmapFactory.decodeFile(
        selectedImageUri.toString(), options); 

      int width = bMapRotate.getWidth(); 
      int height = bMapRotate.getHeight(); 
      pic1 = (ImageView) findViewById(R.id.ivPic1); 
      int viewHeight = pic1.getLayoutParams().height; 
      int viewWidth = pic1.getLayoutParams().width; 
      if (width > height) 
       viewHeight = height; 
      else 
       viewWidth = width; 
      if (bMapRotate != null) { 
       pic1.setImageBitmap(bMapRotate); 
      } 
     } 
    } 
} 

我也試着這樣說:

MediaScannerConnection.scanFile(this, 
    new String[] { outputFileUri.toString() }, null, 
    new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
      setImage(selectedImageUri); 
     } 
    }); 

insted的的

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
        outputFileUri)); 
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED, 
        outputFileUri)); 

,然後有在setImage方法setImage代碼,這應該是在掃描完成時調用,但它仍然不等待掃描完成?這只是一個單獨的文件誰被掃描,所以我不認爲它應該是那麼複雜?我應該使用AsyncTask還是您有其他建議?

編輯: 事實證明,這不是問題。問題在於Uri在路徑前面有「file:/」,這並不是代碼的其餘部分應該如何閱讀。

+0

'我無法讓應用程序等待MediaScanner完成對我新拍攝圖像的掃描。請告訴你爲什麼要這樣做,因爲我不明白你的問題。 – greenapps 2014-10-31 09:25:50

+0

因爲它會引發nullPointerException,因爲應用程序無法從相機中找到圖像。如果我沒有媒體掃描儀,並且我去了畫廊,我的圖像不會在那裏創建。 – 2014-10-31 09:27:59

+1

也許你應該多花一點時間來提出問題,因爲這不是非常有用的信息 – 2015-09-22 20:14:06

回答

0

根據您的onActivityResult塊,我看到你的原意使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE。

看看這個page。它顯示瞭如何將Uri與設備相機的Intent一起包含以保存圖像文件。

至於如何得到一個開放的,看here

總之,如果您指定的路徑要保存的圖像文件,你可以參考你的onActivityResult該路徑()。該文件在MediaStore中可能沒有相應的條目,但它應該可讀並可用於放入ImageView。

+0

我已經應用了這個,當我使用相機時,我只是產生一個空白圖像。 – 2014-10-31 10:45:02