简体   繁体   中英

Why doesn't ImageView load my image?

I'm new to Android development so forgive my lack of knowledge on this. I'm trying to make an app that finds an image stored in internal memory and displays it in an ImageView. I've gone through multiple ways to do this but the image is never displayed.

Here's my code so far:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    tv = (TextView)findViewById(R.id.textView);

    iv = (ImageView)findViewById(R.id.FrontImage);
    bv = (ImageView)findViewById(R.id.BackImage);
    Bitmap myBitmap;
    File imgFile = new  File(PATH,"front1.png");
    //check if image exists
    if(imgFile.exists())
    {
              //get image
            myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            iv.setImageBitmap(myBitmap); //add image to ImageView
            tv.setText(imgFile.getAbsolutePath());
    }            
}

I think the TextView tv displays the correct path but the image doesn't load. Can anybody help me with this?

您需要在清单中添加读取权限,否则,myBitmap始终为null。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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