采用Android相机意图,越来越URI,然后使用URI(Using android camera

2019-07-31 04:05发布

我有如下因素代码:

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


} 

现在的问题是如何获取的图像路径(它保存到我的数据库),然后再次,用它在图片显示(我不知道怎么弄的路径字符串,然后重新使用它)

Answer 1:

为了得到图像路径在onActivityResult则需要通过发送图片的路径,意图以启动相机:

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

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


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

}


文章来源: Using android camera intent, getting uri, and then using uri