为什么未在Android中保存相机图像?

时间:2016-01-04 10:05:03

标签: android camera image-capture

我有一个按钮,当点击它时,它会启动相机并保存我拍摄的图像,但是有些不起作用。 此代码来自here

private void takeAPic() {
    //camera stuff
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
        File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyImages");
        imagesFolder.mkdirs();

    File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
    Uri uriSavedImage = Uri.fromFile(image);
    log("uriSavedImage: "+uriSavedImage.toString());
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    getActivity().startActivityForResult(imageIntent, Constant.REQUEST_CODE.TAKE_A_PIC);
}

记录:uriSavedImage:file:///storage/emulated/0/Pictures/MyImages/QR_20160104_165527.png

我拍摄的照片未保存在手机上,此应用程序返回空数据:

{who=null, request=62, result=-1, data=Intent {  }}

在清单中:

 <uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"></uses-feature>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

onActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {        
    case Constant.REQUEST_CODE.TAKE_A_PIC:
        if (resultCode == Activity.RESULT_OK) {
            setSystemImage(data);
        }
        break;
    default:
        break;
}



private void setSystemImage(Intent data) {
    Uri targetUri = data.getData();
    log("log: "+targetUri.toString());
    try {
        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
        image.setImageBitmap(bitmap);

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

1 个答案:

答案 0 :(得分:0)

尝试在

中添加super.onActivityResult(requestCode, resultCode, data);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

喜欢

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
 }
}