Android Camera:数据意图返回null

sof*_*per 128 android android-camera android-imageview

我有一个包含多个活动的android应用程序.

在其中一个我正在使用一个按钮,它将调用设备摄像头:

public void onClick(View view) {
    Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoIntent, IMAGE_CAPTURE);
}
Run Code Online (Sandbox Code Playgroud)

在同一个活动中,我调用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是intent data是null并且OnActivityResult方法直接转到,(resultCode == RESULT_CANCELED)并且应用程序返回到先前的avtivity.

如何修复此问题,并在调用相机后,应用程序返回当前活动,其中包含ImageView将包含拍摄的照片?

谢谢

Bri*_*sky 208

只有在返回的Intent中传回缩略图时,默认的Android相机应用程序才会返回非空的intent.如果传递EXTRA_OUTPUT了要写入的URI,它将返回一个nullintent,并且该图片位于您传入的URI中.

您可以通过查看GitHub上的相机应用程序源代码来验证这一点:

我猜你要么以EXTRA_OUTPUT某种方式传递,要么手机上的相机应用程序工作方式不同.

  • 我将如何修复put Extra功能,它在相机onActivityResult中提供非null意图 (11认同)
  • 我们是否可以禁用相机类,只需更改一种方法以允许它们,或者它是否更难? (3认同)

小智 16

我找到了一个简单的答案.有用!!

Ø

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();
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您想裁剪和缩放此图像

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 它在现代SDK版本上提供了`FileUriExposedException`. (4认同)

Jor*_*sys 9

我遇到过这个问题,intent不是空的,但是通过这个发送的信息intent没有收到onActionActivit()

在此处输入图片说明

这是使用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);

  }
Run Code Online (Sandbox Code Playgroud)

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();
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

图像描述简介 图像描述简介

在 github 中查看我的示例:

https://github.com/Jorgesys/TakePicture


小智 5

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

-此回复中包含所有已更改的代码;接近android教程

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

官方的android教程“简单地拍摄照片”原来并不完全符合它的承诺。那里提供的代码在我的设备上不起作用:运行Android版本4.4.2 / KitKat / API Level 19的Samsung Galaxy S4 Mini GT-I9195。

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

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
Run Code Online (Sandbox Code Playgroud)

结果导致意图onActivityResult被null 抓住。

为了解决这个问题,我从这里的早期回复和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();
        }
Run Code Online (Sandbox Code Playgroud)

我创建了一个示例应用程序,该应用程序具有拍摄照片,将其保存在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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

请注意,我为该问题找到的解决方案也导致了android清单文件的简化:由于我不在Java代码中使用任何内容,因此不再需要android教程建议的关于添加提供程序的更改。因此,只需将几行标准行(主要是关于权限的行)添加到清单文件中。

需要特别指出的是,Android Studio的自动导入功能可能无法处理java.text.SimpleDateFormatjava.util.Date。我必须手动导入它们。

  • 该问题明确指出,传递给onActivityResult()的名为“ data”的Intent参数为null,然后您的解决方案继续尝试使用该参数。 (2认同)

Man*_*oba -1

以下代码对我有用:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
Run Code Online (Sandbox Code Playgroud)

这是结果:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{ 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    if(resultCode == RESULT_OK)
    {
        Uri selectedImage = imageReturnedIntent.getData();
        ImageView photo = (ImageView) findViewById(R.id.add_contact_label_photo);
        Bitmap mBitmap = null;
        try
        {
            mBitmap = Media.getBitmap(this.getContentResolver(), selectedImage);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)