Android - 图像视图不显示照片

时间:2013-02-15 09:10:55

标签: java android image

快速提问,这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/our_photo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/image_description"/>

<Button
    android:id="@+id/our_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_photo"
    android:onClick="TakePhoto" />

<Button
    android:id="@+id/load_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/show_photo"
    android:onClick="ShowPhoto" />

<TextView
    android:id="@+id/our_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/file_directory" />

 </LinearLayout>

这是我的代码,我尝试显示从库加载的图像(刚刚创建了图像,因此它确实存在,它加载它 - 在调试模式下我可以看到myBitmap对象不是null)但它不显示任何内容。我试图创建一个黑色位图只是为了看它是否是一个布局缺陷,但它显示它很好,它只是目前不能用于文件。

public void ShowPhoto(View v) {
    File imgFile = new  File(mCurrentPhotoPath);
    if(imgFile.exists()){
         //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeStream(new FileInputStream    (imgFile),null,o);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        //The new size we want to scale to
        final int REQUIRED_SIZE_X=240;
        final int REQUIRED_SIZE_Y=400;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE_X && o.outHeight/ scale/2>=REQUIRED_SIZE_Y)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Bitmap myBitmap=null;
        try {
            myBitmap = BitmapFactory.decodeStream(new FileInputStream(imgFile), null, o2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Display display = getWindowManager().getDefaultDisplay();
        Point screenSize = new Point();
        display.getSize(screenSize);
        //imageView.setMinimumHeight(screenSize.y/2);
        //imageView.setMinimumWidth(screenSize.x/2);

        imageView.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, screenSize.x/2, screenSize.y/2, false));
    }

在调试模式下没有异常等等,一切顺利,没有结果。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您好像忘记引用ImageView

ImageView imageView = (ImageView) findViewById(R.id.our_photo);