三星S3中onActivityResult(int requestCode, int resultCode, Intent data)方法中相机意图数据为空。

13
问题:我在三星S3上使用相机意图时,在onActivityResult(int requestCode, int resultCode, Intent data)函数中获取到的数据为null,但在其他设备上运行良好。我已经按照自己的需求编写了代码并在网上搜索过此问题,但没有发现有用的信息。

代码:

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

    }

请在此处放置您的LOGCAT数据。我之前在S3上遇到了同样的问题,通过在AndroidMenifest.xml文件中添加以下权限进行修复 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> - user1621629
很不幸,当图片无法加载时我没有得到任何日志记录。同时,在我的清单文件中也有SD卡权限。 - Akhilesh Mani
4个回答

3

2

我曾经遇到过同样的问题,通过以下方法解决了它,这一定会帮助到你,请尝试:

为了解决这个问题,我参考了下面的链接:

三星 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;

@Akhilesh Mani:找到解决方案了吗? - Bhavesh Patadiya
嗨Bhavesh,这在我的设备上不起作用。在捕获图像后,移动设备方向会自动更改,我的URI变为空。我还将该URI外部存储,但这也不起作用。 - Akhilesh Mani
@AkhileshMani:你是否在清单文件中为该活动设置了这一行“android:configChanges="keyboardHidden|orientation"”,如果没有,请添加它,并调用onConfigurationChanged()方法。 - Bhavesh Patadiya
你可以在这里查看我的回答,附带完整的Java文件。在那里你会有更好的理解。 - Bhavesh Patadiya

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卡上的项目并使用它。

你能否同时提供从SD卡中获取某些内容的代码? - Michael Alan Huff

0
你可能已经得到了你问题的解决方案,但这会对其他人有所帮助。以下是由Commonsware创建的关于拍照后图像裁剪的博客链接。他说使用操作com.android.camera.action.CROP可能不会在所有不同类型的设备中完美地工作。
这里是链接

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接