拍摄照片并没有奏效

时间:2015-04-07 05:59:38

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

3 个答案:

答案 0 :(得分:0)

试试this tutorial。它对我来说很完美。只需下载源代码并尝试运行。

答案 1 :(得分: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设备。

答案 2 :(得分: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); 
}