【问题标题】:getting captured photo didn't work拍摄照片无效
【发布时间】:2015-04-07 05:59:38
【问题描述】:

我想让用户从相机捕获图像并上传到服务器。所以,我需要一个来自捕获照片的图像文件。

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

我尝试了以下操作:

        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。我在网上尝试了几乎所有的解决方案,但没有奏效。任何人都可以对此有任何解决方案吗?

【问题讨论】:

  • @MrNice 你好,你能在回答中解释而不是在评论中解释吗?
  • 你不会像这样得到捕获图像的Uri,你必须在调用startActivityForResult(camera_intent, 1)时传递一个图像路径,然后获取该图像路径。
  • @Sharpedge 你能解释一下吗?

标签: java android android-camera android-contentprovider android-cursor


【解决方案1】:

试试this tutorial。它对我来说非常有效。只需下载源代码并尝试运行。

【讨论】:

    【解决方案2】:

    我有同样的问题。我能够使用 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。它适用于 Google Glass,但可以在任何 Android 设备上正常运行。

    【讨论】:

      【解决方案3】:

      先获取图片路径:

      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); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        相关资源
        最近更新 更多