简体   繁体   English

android相机意图不返回任何数据

[英]android camera intent returns no data

I have a problem with obtaining data from on the 'onActivityResult' method after taking a picture from the camera intent. 从摄像机意图中拍摄照片后,我遇到了从'onActivityResult'方法获取数据的问题。

Here is my method which is calling camera Intent: 这是我调用相机Intent的方法:

private void __initCamera() {

    String path = Environment.getExternalStorageDirectory() + "/vault/images/";

    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();
        dir = new File(path);
    }
    String[] content = dir.list();
    String fileName = (content.length + 1) + ".jpg";
    File file = new File(path, fileName);
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Log.e("my_app", ex.getMessage());
        }
    }

    Uri uri = Uri.fromFile(file);  
    Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
    i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(i, 8765);
}

Here is my onActivityResult method: 这是我的onActivityResult方法:

returnCode: -1(should be: 1, right?), returnCode: -1(should be: 1, right?),

exception: java.io.FileNotFoundException: No content provider: #Intent;action=inline-data;end exception: java.io.FileNotFoundException: No content provider: #Intent;action=inline-data;end

data.toURI() : #Intent;action=inline-data;end data.toURI(): #Intent;action=inline-data;end

   @Override
protected void onActivityResult(int requestCode, int returnCode, Intent data) {

    try {

        Bitmap bitmap = Media.getBitmap(getContentResolver(), Uri.parse(data.toURI()));
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
        bitmap.recycle();
        out.close();
    } catch (Exception ex) {
        Log.e("TROLL", ex.toString());
    }
}

This: 这个:

Bitmap bitmap = Media.getBitmap(getContentResolver(), Uri.parse(data.toURI()));

Should be this: 应该是这样的:

Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 

At least, that's how I normally retrieve images. 至少,这就是我通常检索图像的方式。 Also, I would recommend checking for the correct request code instead of using an all encompassing Exception there. 此外,我建议检查正确的请求代码,而不是使用包含所有包含的异常。

The accepted answer only provides a way to obtain a thumbnail of image, not the full size one. 接受的答案仅提供获取图像缩略图的方法,而不是完整大小的缩略图。 To get the full size image you should provide a uri where camera will store the image like this: 要获得完整尺寸的图像,您应该提供一个uri,相机将存储图像,如下所示:

intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);

Then in your onActivityResult you can use your saved uri to get the image: 然后在你的onActivityResult你可以使用你保存的uri来获取图像:

Bitmap bitmap = Media.getBitmap(getContentResolver(), Uri.parse(mUri));

Here's the Android documentation . 这是Android 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM