13

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

代码 :

    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

4 回答 4

3

如果您提供 MediaStore.EXTRA_OUTPUT,则意图为空,但您将在您提供的文件中包含照片 (Uri.fromFile(f))。

我希望下面的链接为您提供解决方案。它对我有用。

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

于 2014-04-12T09:01:47.680 回答
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;
于 2013-01-31T14:46:30.527 回答
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 卡中的项目并使用它。

于 2013-01-31T14:23:30.630 回答
0

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

这是链接

于 2015-01-15T13:11:16.750 回答