Using android camera intent, getting uri, and then

2019-04-16 23:38发布

问题:

I have a folowing code:

public void take_picture(View view)
{


 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
 startActivityForResult(cameraIntent, CAMERA_REQUEST); 
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
    if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {  

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        slikaa.setImageBitmap(photo);


} 

Now my question is how to get that image path(for saving it to my database), and then again, use it to show in a picture(I don't know how to get String paths, and then re-use it)

回答1:

For getting Image Path in onActivityResult you will need to Start camera by send Image Path with Intent as:

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  

        //ContentValues values = new ContentValues();  
        ContentValues values = new ContentValues(3);  
        values.put(MediaStore.Images.Media.DISPLAY_NAME, "testing");  
        values.put(MediaStore.Images.Media.DESCRIPTION, "this is description");  
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");  
        imageFilePath = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath); 

        startActivityForResult(intent, CAMERA_REQUEST); 

and on onActivityResult

            protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
           ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
            if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {  
           //get image from path

            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            photo = BitmapFactory.decodeStream(this.getContentResolver()  
                .openInputStream(imageFilePath), null, op);  
            slikaa.setImageBitmap(pic);  

            //slikaa.setImageBitmap(photo);
          } 


回答2:

String path;

Public void take_picture(){

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File output = new File(dir,"gtumca.png");
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
    path = output.getAbsolutePath(); <-------------
    startActivityForResult(cameraIntent, TAKE_PHOTO);

}