Android相机:数据意图返回空值

148
我有一个包含多个活动的Android应用程序。
其中一个活动中,我使用了一个按钮,点击它会调用设备相机。
public void onClick(View view) {
    Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoIntent, IMAGE_CAPTURE);
}

在同一个活动中,我调用onActivityResult方法来获取图像结果。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK) {
            Bitmap image = (Bitmap) data.getExtras().get("data");
            ImageView imageview = (ImageView) findViewById(R.id.pic);
            imageview.setImageBitmap(image);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "CANCELED ", Toast.LENGTH_LONG).show();
        }
    }
}

问题是意图data为空,onActivityResult方法直接转到(resultCode == RESULT_CANCELED),应用程序返回到上一个活动。
我该如何解决这个问题,使得调用相机后,应用程序返回到包含拍摄照片的当前活动,其中包含一个ImageView

请查看此答案:https://dev59.com/JnXYa4cB1Zd3GeqP6G_k#18207723 - Praveen Sharma
11个回答

251
默认的Android相机应用程序仅在返回缩略图时才返回非空意图。如果您传递了一个要写入的URI的EXTRA_OUTPUT,它将返回一个null意图,并且图片位于您传递的URI中。您可以通过查看GitHub上相机应用程序的源代码来验证这一点。 我猜你可能会以某种方式传递 EXTRA_OUTPUT,或者你手机上的相机应用程序工作方式不同。

15
我该如何修复“putExtra”功能,在相机“onActivityResult”中提供非空Intent的问题? - Abdul Wahab
我修复了指向Gingerbread的链接,但不确定它在最新版本中的位置。 - Brian Slesinsky
问题:当您指定EXTRA_OUTPUT参数时,是否预期出现RESULT_CANCELED? - Johnny Z
4
我们能继承相机类并仅更改那个方法以允许两者,还是比那更难? - Tanner Summers
1
这有什么道理呢?如果数据对象为空,那么由于它是空的,你应该如何获取作为数据对象一部分的额外信息呢? - szaske
显示剩余6条评论

19

我找到了一个简单的答案。它有效!

private void openCameraForResult(int requestCode){
    Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri  = Uri.parse("file:///sdcard/photo.jpg");
    photo.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(photo,requestCode);
}

if (requestCode == CAMERA_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            File file = new File(Environment.getExternalStorageDirectory().getPath(), "photo.jpg");
            Uri uri = Uri.fromFile(file);
            Bitmap bitmap;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                bitmap = cropAndScale(bitmap, 300); // if you mind scaling
                profileImageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

如果您希望裁剪并缩放此图像

public static  Bitmap cropAndScale (Bitmap source, int scale){
    int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth();
    int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth();
    int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2;
    int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2;
    source = Bitmap.createBitmap(source, x, y, factor, factor);
    source = Bitmap.createScaledBitmap(source, scale, scale, false);
    return source;
}

7
在现代 SDK 版本上会出现“FileUriExposedException”的异常。 - Souradeep Nanda
你需要定义androidx.core.content.FileProvider和@xml/filepath。 - Duna
谢谢,这对我有用。但是我使用了 contentResolver 来创建路径。 - Parthan_akon

14
我曾经遇到过这个问题,intent不为空,但是通过这个intent发送的信息在onActivityResult()中没有收到。

enter image description here

这是一个更好的解决方案,使用getContentResolver()
    private Uri imageUri;
    private ImageView myImageView;
    private Bitmap thumbnail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
      ...
      ...    
      ...
      myImageview = (ImageView) findViewById(R.id.pic); 

      values = new ContentValues();
      values.put(MediaStore.Images.Media.TITLE, "MyPicture");
      values.put(MediaStore.Images.Media.DESCRIPTION, "Photo taken on " + System.currentTimeMillis());
      imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
      startActivityForResult(intent, PICTURE_RESULT);
  }
onActivityResult() 方法通过 getContentResolver() 获取存储的位图:
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {

             Bitmap bitmap;
             try {
                 bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                 myImageView.setImageBitmap(bitmap);
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }

introducir la descripción de la imagen aquí introducir la descripción de la imagen aquí

在GitHub上查看我的示例:

https://github.com/Jorgesys/TakePicture


每次使用它都会将一个新文件放在/.../Pictures上。有什么方法可以避免这种情况吗? - android developer
这需要外部存储写入权限。 - Damercy

7

简单的工作相机应用程序,避免空意图问题

- 所有更改的代码均包含在此答复中;接近于Android教程

我花了很多时间解决这个问题,所以我决定创建一个帐户并与您分享我的结果。

官方的 Android 教程“简单拍照”没有完全实现其承诺。 那里提供的代码在我的设备上无法正常工作:三星 Galaxy S4 Mini GT-I9195 运行 Android 版本 4.4.2 / KitKat / API 级别 19。

我发现问题的主要原因是在捕获照片时调用的方法中的以下行(教程中的dispatchTakePictureIntent):

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

这导致onActivityResult捕获到的意图为空。

为了解决这个问题,我从此前在这里得到的回复和一些有用的Github帖子中获取了很多灵感(主要是由deepwinter撰写的这篇文章 - 非常感谢他;你可能也想看看他在一个密切相关的帖子上的回复)。

遵照这些建议,我选择了删除提到的putExtra行,并在onActivityResult()方法中执行相应的操作,从相机中获取拍摄的图片。

获取与图片相关联的位图的决定性代码行如下:

        Uri uri = intent.getData();
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }

我创建了一个范例应用程序,它只具备拍照、保存到SD卡并显示的功能。我认为对于像我一样在遇到这个问题时处于同样困境的人们会有所帮助,因为当前的帮助建议大多参考了相当复杂的GitHub帖子,虽然能解决问题,但对于像我这样的新手来说不太容易理解。
就默认情况下Android Studio创建新项目时的文件系统而言,我只需更改三个文件即可实现我的目的:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.simpleworkingcameraapp.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="takePicAndDisplayIt"
    android:text="Take a pic and display it." />

<ImageView
    android:id="@+id/image1"
    android:layout_width="match_parent"
    android:layout_height="200dp" />

</LinearLayout>

MainActivity.java :

package com.example.android.simpleworkingcameraapp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

private ImageView image;
static final int REQUEST_TAKE_PHOTO = 1;
String mCurrentPhotoPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView) findViewById(R.id.image1);
}

// copied from the android development pages; just added a Toast to show the storage location
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_LONG).show();
    return image;
}

public void takePicAndDisplayIt(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        File file = null;
        try {
            file = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultcode == RESULT_OK) {
        Uri uri = intent.getData();
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        image.setImageBitmap(bitmap);
    }
}
}

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.simpleworkingcameraapp">


<!--only added paragraph-->
<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  <!-- only crucial line to add; for me it still worked without the other lines in this paragraph -->
<uses-permission android:name="android.permission.CAMERA" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

请注意,我为该问题找到的解决方案还简化了Android清单文件:由于我没有在Java代码中使用任何提供程序,因此不再需要按照Android教程建议的更改。因此,只需添加一些标准行 - 主要涉及权限 - 到清单文件即可。
另外,值得指出的是,Android Studio的自动导入可能无法处理java.text.SimpleDateFormat和java.util.Date。我不得不手动导入它们。

此解决方案需要存储权限,这并不是一个很好的方法。 - shanraisshan
4
问题明确指出传递给onActivityResult()的名为"data"的Intent参数为空,但您的解决方案却试图使用它。 - Maks
方法"MediaStore.Images.Media.getBitmap"已被弃用。 - undefined

1
可能是因为您有类似这样的东西?
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                        
Uri fileUri =  CommonUtilities.getTBCameraOutputMediaFileUri();                  
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);                        
startActivityForResult(takePictureIntent, 2);

但是你不能将额外的输出放入意图中,因为这样数据会进入URI而不是数据变量。因此,你必须删除中间的两行,以便你有

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 2);

那就是导致我的问题的原因,希望有所帮助。

14
这个回答有误导性。如果没有使用MediaStore.EXTRA_OUTPUT,你将只会得到缩略图而不是全尺寸照片。这在文档和其他答案中都有强调。 - user2417480
3
这个答案绝对是误导性的。 - IndexOutOfDevelopersException
这是一个误导性的答案。 - Abdulla Thanseeh

0

0
我使用了contentResolver来创建路径,它起作用了。
var values = ContentValues()
            values.put(MediaStore.Images.Media.TITLE, "MyPicture")
            values.put(
                MediaStore.Images.Media.DESCRIPTION,
                "Photo taken on " + System.currentTimeMillis()
            )
            cameraUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, cameraUri);
            startActivityForResult(cameraIntent, REQUEST_CODE)


0

即使到了2022年,它仍然会发生,我按照以下方式修复:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // android7.0
                    photoUri= FileProvider.getUriForFile(
                            MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            new File(mCameraFilePath));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                } else {
                    photoUri = Uri.fromFile(new File(mCameraFilePath));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
                }

哦...只需在您的活动中添加一个变量(photoUri)!然后

filePathCallback.onReceiveValue(new Uri[]{ photoUri });

0

适用于我的 Kotlin 代码:

 private fun takePhotoFromCamera() {
          val intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
        startActivityForResult(intent, PERMISSIONS_REQUEST_TAKE_PICTURE_CAMERA)
      }

并获取结果:

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
 if (requestCode == PERMISSIONS_REQUEST_TAKE_PICTURE_CAMERA) {
         if (resultCode == Activity.RESULT_OK) {
           val photo: Bitmap? =  MediaStore.Images.Media.getBitmap(this.contentResolver, Uri.parse( data!!.dataString)   )
            // Do something here : set image to an ImageView or save it ..   
              imgV_pic.imageBitmap = photo 
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.i(TAG, "Camera  , RESULT_CANCELED ")
        }

    }

}

不要忘记声明请求代码:

companion object {
 const val PERMISSIONS_REQUEST_TAKE_PICTURE_CAMERA = 300
  }

0
经过多次尝试和学习,我终于弄明白了。首先,来自Intent的变量数据始终为空,因此检查!null将导致应用程序崩溃,只要您将URI传递给startActivityForResult。请参照以下示例。我将使用Kotlin。
  1. 打开相机意图

    fun addBathroomPhoto(){
    addbathroomphoto.setOnClickListener{
    
        request_capture_image=2
    
        var takePictureIntent:Intent?
        takePictureIntent =Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if(takePictureIntent.resolveActivity(activity?.getPackageManager()) != null){
    
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                // 创建文件时出错
    
                null
            }
    
            if (photoFile != null) {
                val photoURI: Uri = FileProvider.getUriForFile(
                    activity!!,
                    "ogavenue.ng.hotelroomkeeping.fileprovider",photoFile)
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    photoURI);
                startActivityForResult(takePictureIntent,
                    request_capture_image);
            }
    
    
        }
    
    }
    

    }

  2. 创建createImageFile()方法。但是必须将imageFilePath变量设置为全局变量。在Android官方文档中有创建示例,非常简单。

  3. 获取Intent

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    
    if (requestCode == 1 && resultCode == RESULT_OK) {
        add_room_photo_txt.text=""
        var myBitmap=BitmapFactory.decodeFile(imageFilePath)
        addroomphoto.setImageBitmap(myBitmap)
        var file=File(imageFilePath)
        var fis=FileInputStream(file)
        var bm = BitmapFactory.decodeStream(fis);
        roomphoto=getBytesFromBitmap(bm) }}
    
  4. getBytesFromBitmap方法

      fun getBytesFromBitmap(bitmap:Bitmap):ByteArray{
    
      var stream=ByteArrayOutputStream()
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
      return stream.toByteArray();
      }
    

希望这能有所帮助。


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