1

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

<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 对象不是空的)但它不显示任何事物。我试图创建一个黑色位图只是为了查看它是否是布局缺陷,但它显示它很好,它目前不适用于文件。

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

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

4

1 回答 1

0

看起来您忘记引用您的ImageView.

ImageView imageView = (ImageView) findViewById(R.id.our_photo);
于 2013-02-15T09:30:55.470 回答