2014-10-31 63 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