简体   繁体   中英

how to set captured image on imageview

How would I set the image captured on the imageview using getData? When I run it, click ok the application closes.

 if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
             if (resultCode == RESULT_OK) {



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


                 String[] projection = {};
                 Cursor cursor = getContentResolver().query(fileUri, projection, null, null, null);
                 int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                 cursor.moveToFirst();
                 String capturedImageFilePath = cursor.getString(column_index_data);

You can try this

Append this code after your getting path

File imgFile = new  File(capturedImageFilePath );

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

Use this:

if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
         if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
         Log.d("selectedimage", ""+selectedImage);
         String[] filePath = { MediaStore.Images.Media.DATA };
         Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
         c.moveToFirst();
         int columnIndex = c.getColumnIndex(filePath[0]);
         String picturePath = c.getString(columnIndex);
         c.close();
         Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));

         Log.w("path of image from gallery......******************.........", picturePath+"");
         ImageView m1= (ImageView)  findViewById(R.id.imageView1);
}
}

You need to get the image path from data, your code should look like this:

Uri uri = data.getData();
String path = convertMediaUriToPath(getApplicationContext(), uri);
Bitmap b = BitmapFactory.decodeFile(path);
img.setImageBitmap(b);

where

public String convertMediaUriToPath(Context context, Uri uri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.MediaColumns.DATA};
        cursor = context.getApplicationContext()
                .getContentResolver().query(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        Log.e("path", path);
        return path;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM