开发者

Display the latest picture taken in the image view layout in android

开发者 https://www.devze.com 2023-03-08 09:47 出处:网络
I am working on a app that takes a picture with my camera and shows it in my image view layout.So any one can te开发者_运维百科ll me how to access the latest picture taken and display it.i got the sol

I am working on a app that takes a picture with my camera and shows it in my image view layout.So any one can te开发者_运维百科ll me how to access the latest picture taken and display it.


i got the solution if anyone have the same problem u can consult this

public void click1(View v){
    //define the file-name to save photo taken by Camera activity
    capturedImageFilePath=null;
    fileName = System.currentTimeMillis()+"";
    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
    //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);


    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            //use imageUri here to access the image
             String[] projection = { MediaStore.Images.Media.DATA}; 
                Cursor cursor = managedQuery(imageUri, projection, null, null, null); 
                int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
                cursor.moveToFirst(); 
                capturedImageFilePath = cursor.getString(column_index_data);
                imageFile = new File(capturedImageFilePath);
                if(imageFile.exists()){
                    Bitmap bm = BitmapFactory.decodeFile(capturedImageFilePath);
                    image.setImageBitmap(bm);}
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        } else {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        }
    }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消