0

我想让用户从相机捕获图像并上传到服务器。所以,我需要捕获的照片中的图像文件。获取拍摄照片无效

我搜索了很多解决方案,但都没有工作。

我尝试以下操作:

 Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (camera_intent.resolveActivity(getActivity().getPackageManager()) != null) { 
              startActivityForResult(camera_intent, 1); 
       } 
在onActivityResult

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent dataIntent) { 
     super.onActivityResult(requestCode, resultCode, dataIntent); 

     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == 1) { 
       Bundle dataBundle = dataIntent.getExtras(); 
       Bitmap imageBitmap = (Bitmap) dataBundle.get("data"); 
       img_profile.setImageBitmap(imageBitmap); //getting thumbnail and setting to preview image 

     try{ 
       Uri selectedImageUri = dataIntent.getData(); 

       String[] projection = { MediaStore.Images.Media.DATA }; 
       Cursor cursor = getActivity().getContentResolver().query(selectedImageUri, projection, null, null,null); 

       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 

       String filePath = cursor.getString(column_index); 

       profilePicFile = new File(filePath); 

       cursor.close(); 
      } 
      catch (Exception e){ 
       e.printStackTrace(); 
      } 

,但我我得到了nullPinterException说Uri is null。我在网上尝试了几乎所有的解决方案,但都没有奏效。任何人都可以有任何解决方案吗?

+0

@MrNice你好,你能不能回答,而不是在评论解释? –

+0

您不会像这样获取捕获的图像的Uri,您在调用'startActivityForResult(camera_intent,1)'时必须传递图像路径,然后获取该图像路径。 –

+0

@Sharpedge你能解答吗? –

回答

0

尝试this tutorial。它对我来说非常合适。只需下载源代码并尝试运行即可。

0

我有同样的问题。我能够使用FileObserver解决此问题。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) { 
     String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH); 
     String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); 

     processPictureWhenReady(picturePath); 
     // TODO: Show the thumbnail to the user while the full picture is being 
     // processed. 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 

private void processPictureWhenReady(final String picturePath) { 
    final File pictureFile = new File(picturePath); 

    if (pictureFile.exists()) { 
     // The picture is ready; process it. 
    } else { 
     // The file does not exist yet. Before starting the file observer, you 
     // can update your UI to let the user know that the application is 
     // waiting for the picture (for example, by displaying the thumbnail 
     // image and a progress indicator). 

     final File parentDirectory = pictureFile.getParentFile(); 
     FileObserver observer = new FileObserver(parentDirectory.getPath(), 
       FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) { 
      // Protect against additional pending events after CLOSE_WRITE 
      // or MOVED_TO is handled. 
      private boolean isFileWritten; 

      @Override 
      public void onEvent(int event, String path) { 
       if (!isFileWritten) { 
        // For safety, make sure that the file that was created in 
        // the directory is actually the one that we're expecting. 
        File affectedFile = new File(parentDirectory, path); 
        isFileWritten = affectedFile.equals(pictureFile); 

        if (isFileWritten) { 
         stopWatching(); 

         // Now that the file is ready, recursively call 
         // processPictureWhenReady again (on the UI thread). 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           processPictureWhenReady(picturePath); 
          } 
         }); 
        } 
       } 
      } 
     }; 
     observer.startWatching(); 
    } 
} 

这里是我参考的link解决问题。它适用于谷歌眼镜,但适用于任何Android设备。

0

为了得到路径图像的第一:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, CAMERA_REQUEST); 

然后写onActivityResult如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
    imageView.setImageBitmap(photo); 
    knop.setVisibility(Button.VISIBLE); 


    // CALL THIS METHOD TO GET THE URI FROM THE BITMAP 
    Uri tempUri = getImageUri(getApplicationContext(), photo); 

    // CALL THIS METHOD TO GET THE ACTUAL PATH 
    File finalFile = new File(getRealPathFromURI(tempUri)); 

    System.out.println(mImageCaptureUri); 
    } 
} 

// this method gets the image URI 
    public Uri getImageUri(Context inContext, Bitmap inImage) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
    return Uri.parse(path); 
} 

public String getRealPathFromURI(Uri uri) { 
Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
cursor.moveToFirst(); 
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
return cursor.getString(idx); 
}