使用Android相机意图,获取uri,然后使用uri

时间:2012-08-25 13:40:32

标签: android camera gallery uri

我有一个以下代码:

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


} 

现在我的问题是如何获取该图像路径(将其保存到我的数据库中),然后再次使用它来显示图片(我不知道如何获取String路径,然后重新使用它)

2 个答案:

答案 0 :(得分:2)

要获取onActivityResult中的Image Path,您需要通过发送带有Intent的Image Path来启动相机:

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

答案 1 :(得分:0)

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

}