簡體   English   中英

三星S3中的onActivityResult(int requestCode,int resultCode,Intent data)中的相機意圖數據為null

[英]camera intent data null in onActivityResult(int requestCode, int resultCode, Intent data) in Samsung S3

問題:我在Samsung S3中的onActivityResult(int requestCode, int resultCode, Intent data)中獲取了相機意圖的數據null。 但在其他一些設備上運行良好。 我自定義了用於獲取數據的代碼,並在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>
        }

    }

如果您提供MediaStore.EXTRA_OUTPUT,則intent為null,但您將在您提供的文件中顯示該照片(Uri.fromFile(f))。

我希望以下鏈接為您提供解決方案。它為我工作。

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

我已經面臨同樣的問題,並通過下面的方式解決它肯定會幫助你一次:

對於解決方案,我在下面提到了解決問題的鏈接:

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

我通過告訴相機活動將圖像保存在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卡中的項目並使用它。

您可能已經得到了問題的解決方案,但這對某人有幫助。 以下是Commonsware創建的博客鏈接,該鏈接是關於拍攝照片后的圖像裁剪。 他說使用動作com.android.camera.action.CROP可能不適用於所有不同類型的設備。

這是鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM