Android MediaStore.ACTION\u IMAGE\u捕获意图返回空数据。getData()

Android MediaStore.ACTION\u IMAGE\u捕获意图返回空数据。getData(),android,camera,null,Android,Camera,Null,我尝试使用以下代码调用Android摄像头: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { Uri uri = data.getData(); if (uri != null) Log.d("", uri.t

我尝试使用以下代码调用Android摄像头:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        if (uri != null) Log.d("", uri.toString());
        else Log.d("", "uri is null."); // ...but why? It should hold the image URI.
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 1);
}
根据文件:

MediaStore.EXTRA_OUTPUT-此设置需要一个Uri对象,指定保存图片的路径和文件名。此设置是可选的,但强烈建议使用。如果未指定此值,相机应用程序将使用返回的intent的intent.getData()字段中指定的默认名称将请求的图片保存在默认位置。”

这不是我的经验。对我来说,这段代码为
data.getData()
返回null。我也尝试过设置输出Uri,但这给了我一整套不同的问题


还有其他人经历过这种情况吗?

看看这段代码

protected void onActivityResult(int requestCode, int resultCode, Intent data)  {
   if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    Bitmap photo = (Bitmap) data.getExtras().get("data");

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "test.jpg");
    try {

        f.createNewFile();
        //write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        // remember close de FileOutput
        fo.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (f.exists()) {

        Toast.makeText(this, "Image Found : "+f.getAbsolutePath().toString(), Toast.LENGTH_SHORT).show();

    }else{
          Toast.makeText(this, "Image Not Found", Toast.LENGTH_SHORT).show();
    }


 }
}
你的意图应该是

Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

只要你的用户可以使用任何相机应用程序,你最好创建临时文件并将URI放入额外的输出。规范不明确,也不是每个应用程序都遵循它。 以下是我用于保留文件的方法:

public static File reserveTempFile(String directory, String extension) throws IOException {
    final File pathFile = new File(directory);
    if (!pathFile.exists()) {
        final boolean result = pathFile.mkdirs();
        if (!result) {
            throw new IOException("Can't create directory");
        }
    }

    File file;
    do {
        String fileName = Long.toString(System.nanoTime());
        if (Utils.isNotEmpty(extension)) {
            fileName += "." + extension;
        }
        file = new File(pathFile, fileName);
    } while (file.exists());

    return file;
}

这是关于什么设备的?我知道我必须根据设备制造商/os版本采取一些变通办法…
数据在某些情况下确实是
null
。一款相当模糊的ToughShield R500+坚固手机:如果
数据本身是
null
,我使用了一个伪硬编码字符串,我知道这台设备正在保存数据图片到…在我遇到的一些设备上,有时
数据本身是空的。在这种情况下,你必须告诉相机,图片保存在哪里,并自己记住uri:是的,完全正确;使用
Intent.putExtra(MediaStore.EXTRA\u OUTPUT,yourcustomuritorember)
。我认为Intent中返回的位图只是一个缩略图。@shkschneider如果您对我的回答满意,请接受它。我已尝试创建
mNewPhoto
文件对象,并调用
Intent.putExtra(MediaStore.EXTRA\u OUTPUT,mNewPhoto.toURI());
但当我从相机返回时,
mNewPhoto.exists()
是假的。我很困惑。