Save Capture Image in SD card android

2019-07-18 04:58发布

问题:

I call camera intent and try to save capture image in sd card.but camera intent give me null data.what is the problem i can't understood.my code is as follow

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);
        }
    }  
}

but i get null data? how to solve my problem?plz help

回答1:

Read this Android Training post carefully to figure things out. As the post says,

The Android Camera application saves a full-size photo if you give it a file to save into.

so if you did pass the Uri of a file, you should access the image using this Uri.



回答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 is a Uri object