简体   繁体   中英

Android bitmap image does not show in imageview

I'm having trouble showing the bitmap image. In my app user can click an Image view and there will be an Alert Dialog prompt to choose to take the picture using camera or get the picture from file. I have an Image view named 'image'. Here are my alert Dialog listener :

if(option == 0) // take a picture option
                    {   
                        try
                        {   
                            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                            ContentValues values = new ContentValues();
                            values.put(MediaStore.Images.Media.TITLE, Constants.PIC_FILENAME_PREFIX + System.currentTimeMillis() + Constants.PIC_FILENAME_SUFFIX);
                            mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

                            captureIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 2 * 1024 * 1024); // limit to 2MB data

                            startActivityForResult(captureIntent, CAMERA_CAPTURE);// start activity
                        } 
                        catch (ActivityNotFoundException anfe)
                        {
                            String errorMessage = "It appears your device does not have camera..";
                            Toast.makeText(UploadPhoto.this, errorMessage, Toast.LENGTH_SHORT).show();

                        }                                       
                    }
                    else if(option==1)
                    {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FROM_FILE);
                    }

And below is my Activity Result :

public void ActivityResult(int request, int result, Intent intentdata)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inInputShareable = true;

        if(result ==RESULT_OK)
        {
            if(request==SELECT_FROM_FILE)
            {
                mCapturedImageURI = intentdata.getData();
            }

            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
            if(null==cursor)
            {
                return;
            }

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            cursor.moveToFirst();
            mCapturedFilePath = cursor.getString(column_index);
            stopManagingCursor(cursor);
            cursor.close();

            // compute the boundary first
            options.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(mCapturedFilePath, options);
            // get image from file
            options.inSampleSize = ImageUtil.calculateInSampleSize(options, Constants.PIC_IMAGE_DISP_WIDTH, Constants.PIC_IMAGE_DISP_HEIGHT);
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(mCapturedFilePath, options);

            bitmap = Bitmap.createScaledBitmap(bitmap, Constants.PIC_IMAGE_DISP_WIDTH, Constants.PIC_IMAGE_DISP_HEIGHT, false);

            image.setImageBitmap(bitmap);//I'm trying to change this imageview
        }
    }

When user clicks on one of the option and takes or chooses file, the imageView does not change. Does anybody have a recommendation?

The method should be

protected void onActivityResult(int requestCode, int resultCode, Intent intent)

if you just typoed here, try adding invalidate() after setting the image.

I have found another solution and its work for me.

from here

final ImageView thumb = (ImageView) findViewById(R.id.thumbnail);
    thumb.post(new Runnable() {
        @Override
        public void run()
        {
            Bitmap bmp = BitmapFactory.decodeFile(image_path);
            thumb.setImageBitmap(bmp);
            Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
        }
    });

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