三星S3中的onActivityResult(int requestCode,int resultCode,Intent data)中的相机意图数据为null

时间:2013-01-31 14:17:37

标签: android

问题:我在三星S3的onActivityResult(int requestCode, int resultCode, Intent data)中获取相机意图的数据为空。但在其他一些设备上运行良好。我自定义了用于获取数据的代码,并在Web中搜索了这个问题但没有任何有用的东西

代码:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == TAKE_CAMERA && data != null && data.getData() != null)  

           else if (requestCode == TAKE_CAMERA) {
        if (resultCode != RESULT_OK) return;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(tempFileUri, "image/*");

        intent.putExtra("outputX", 90);
        intent.putExtra("outputY", 90);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CROP_CAMERA);
    } else if (requestCode == CROP_CAMERA && data != null) {
        Bitmap photo = data.getExtras().getParcelable("data");
        try {
            FileOutputStream out = new FileOutputStream(tempFileUri.getPath());
            photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (photo != null) {
            imagePhoto.setImageBitmap(photo);

            <my code>
        }

    }

4 个答案:

答案 0 :(得分:3)

如果您提供MediaStore.EXTRA_OUTPUT,则intent为null,但您将在您提供的文件中显示该照片(Uri.fromFile(f))。

我希望以下链接为您提供解决方案。它为我工作。

http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html

答案 1 :(得分:2)

我已经面临同样的问题,并通过下面的方式解决它肯定会帮助你解决一次:

对于解决方案,我在下面提到了解决问题的链接:

Camera Solution for Samsung Galaxy S3

在调用捕获的图像时写下面的代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);

通过以下方式在OnActivityResult()方法中获取您的URI方法:

if (resultCode != RESULT_OK)
            return;

        switch (requestCode) {
        case PICK_FROM_CAMERA:
            Log.i("TAG", "Inside PICK_FROM_CAMERA");


            // Describe the columns you'd like to have returned. Selecting from
            // the Thumbnails location gives you both the Thumbnail Image ID, as
            // well as the original image ID

            try {
                Log.i("TAG", "inside Samsung Phones");
                String[] projection = {
                        MediaStore.Images.Thumbnails._ID, // The columns we want
                        MediaStore.Images.Thumbnails.IMAGE_ID,
                        MediaStore.Images.Thumbnails.KIND,
                        MediaStore.Images.Thumbnails.DATA };
                String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
                                                                                // only
                                                                                // mini's
                        MediaStore.Images.Thumbnails.MINI_KIND;

                String sort = MediaStore.Images.Thumbnails._ID + " DESC";

                // At the moment, this is a bit of a hack, as I'm returning ALL
                // images, and just taking the latest one. There is a better way
                // to
                // narrow this down I think with a WHERE clause which is
                // currently
                // the selection variable
                Cursor myCursor = this.managedQuery(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        projection, selection, null, sort);

                long imageId = 0l;
                long thumbnailImageId = 0l;
                String thumbnailPath = "";

                try {
                    myCursor.moveToFirst();
                    imageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
                    thumbnailImageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
                    thumbnailPath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
                } finally {
                    // myCursor.close();
                }

                // Create new Cursor to obtain the file Path for the large image

                String[] largeFileProjection = {
                        MediaStore.Images.ImageColumns._ID,
                        MediaStore.Images.ImageColumns.DATA };

                String largeFileSort = MediaStore.Images.ImageColumns._ID
                        + " DESC";
                myCursor = this.managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        largeFileProjection, null, null, largeFileSort);
                String largeImagePath = "";

                try {
                    myCursor.moveToFirst();

                    // This will actually give yo uthe file path location of the
                    // image.
                    largeImagePath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
                    mImageCaptureUri = Uri.fromFile(new File(
                            largeImagePath));

                } finally {
                    // myCursor.close();
                }
                // These are the two URI's you'll be interested in. They give
                // you a
                // handle to the actual images
                Uri uriLargeImage = Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        String.valueOf(imageId));
                Uri uriThumbnailImage = Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        String.valueOf(thumbnailImageId));

                // I've left out the remaining code, as all I do is assign the
                // URI's
                // to my own objects anyways...
            } catch (Exception e) {

                Log.i("TAG",
                        "inside catch Samsung Phones exception " + e.toString());

            }


            try {

                Log.i("TAG", "URI Normal:" + mImageCaptureUri.getPath());
            } catch (Exception e) {
                Log.i("TAG", "Excfeption inside Normal URI :" + e.toString());
            }

            //doCrop();

            break;

答案 2 :(得分:1)

我通过告诉相机活动将图像保存在SD卡上来解决了这个问题:

tempImageNameUri = "some temporary name";
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,tempImageNameUri);
i.putExtra("return-data", true);
startActivityForResult(i,SOME_CONSTANT);

当被叫活动结束时,在onActivityResult中从临时路径中取出SD卡中的项目并使用它。

答案 3 :(得分:0)

你可能已经得到了问题的解决方案,但这对某人有帮助。以下是Commonsware创建的博客链接,该链接是关于拍摄照片后的图像裁剪。他说使用动作com.android.camera.action.CROP可能不适用于所有不同类型的设备。

以下是Link