【问题标题】:Android bitmap image does not show in imageviewAndroid位图图像不显示在imageview中
【发布时间】:2013-06-12 06:18:17
【问题描述】:

我无法显示位图图像。在我的应用程序中,用户可以单击图像视图,然后会出现一个警报对话框提示选择使用相机拍照或从文件中获取图片。我有一个名为“图像”的图像视图。这是我的警报对话框侦听器:

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

下面是我的活动结果:

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

当用户单击其中一个选项并获取或选择文件时,imageView 不会更改。有人推荐吗?

【问题讨论】:

  • 有logcat输出吗?

标签: android image onclicklistener


【解决方案1】:

方法应该是

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

如果您只是在这里打错字,请尝试在设置图像后添加 invalidate()。

【讨论】:

  • 我不知道我做了什么,但它现在似乎工作了。感谢随机公民
  • 我有同样的问题,我尝试了你的解决方案,但没有任何运气。任何其他相同的解决方案?
【解决方案2】:

我找到了另一种解决方案及其对我的工作。

来自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();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多