在SD卡android中保存捕获图像

时间:2013-04-24 07:28:44

标签: android android-camera

我调用相机意图并尝试在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);
        }
    }  
}

但我得到空数据?如何解决我的问题?plz help

2 个答案:

答案 0 :(得分: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;
    }

   }


    }
}

cameraImagePathUri对象

答案 1 :(得分:1)

仔细阅读this Android培训帖子以了解情况。正如帖子所说,

  

如果您提供要保存的文件,Android相机应用程序会保存完整尺寸的照片。

因此,如果您确实传递了文件的Uri,则应使用此Uri访问该图片。