2

我调用相机意图并尝试将捕获图像保存在 sd 卡中。但是相机意图给我空数据。我无法理解的问题是什么。我的代码如下

imgBtnPicture.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            String fileName="ashraful.jpg";

            String root = Environment.getExternalStorageDirectory().toString();
            new File(root + "/photofolder").mkdirs();
            File outputfile=new File(root+"/photofolder/", fileName);
            Uri outputFileUri = Uri.fromFile(outputfile);
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    }); 


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

        if(data!=null)
        {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            Bitmap icon1=ResizeBitmap(photo,200,200);
            imgBtnPicture.setImageBitmap(icon1);
        }
    }  
}

但我得到空数据?如何解决我的问题?请帮助

4

2 回答 2

2
private void takeNewPicture() {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ContentValues values = new ContentValues(3);

    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    cameraImagePath = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
    startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Uri imageFilePathUri = null;
    if (cameraImagePath != null) {
        Cursor imageCursor = getContentResolver().query(
            cameraImagePath, filePathColumn, null, null, null);
    if (imageCursor != null && imageCursor.moveToFirst()) {
        int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]);
        String filePath = imageCursor.getString(columnIndex);
        imageCursor.close();
        imageFilePathUri = filePath != null ? Uri
                .parse(filePath) : null;
    }

   }


    }
}

cameraImagePath是一个Uri对象

于 2013-04-24T07:35:51.290 回答
1

仔细阅读这篇Android 培训帖子以找出答案。正如帖子所说,

如果你给它一个文件来保存,Android 相机应用程序会保存一张全尺寸的照片。

所以如果你确实传递了Uri一个文件,你应该使用这个来访问图像Uri

于 2013-04-24T07:35:58.133 回答