java - Android - ImageView 不显示照片

标签 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>

这是我的代码,我尝试显示从图库加载的图像(图像刚刚创建,所以它确实存在,它加载它 - 在 Debug模式下我可以看到 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));
    }

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

最佳答案

您似乎忘记引用您的 ImageView

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

关于java - Android - ImageView 不显示照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891304/

相关文章:

Java 错误 : constructor in class cannot be applied to given types

android - 发布应用程序的内容存储

python - 非标准图像分析 API 失败,出现 urllib2 错误 : Temporary failure in name resolution

java - Gson 从 JSON 到 HashMap<Integer, MyObject> 其中 Integer 位于 JSON 对象内部

java - 如何在Java中的抽象父类的子类中初始化 protected 最终变量?

java - Jenkins groovy 正则表达式匹配字符串 : Error: java. io.NotSerializableException : java. util.regex.Matcher

android - INSTALL_FAILED_MISSING_SHARED_LIBRARY

java - 我正在尝试掌握如何将界面从 android 转换为 Objective-C

python - 当Microsoft Edge进行操作时,如何使用python从PDF渲染图像?

javascript - 设置 Fancybox 图像尺寸