Android: Displaying .jpg image from sdcard using ImageView

你说的曾经没有我的故事 提交于 2019-12-19 09:23:50

问题


I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks

EDIT:

        layout1 = (LinearLayout) findViewById(R.id.layout1Activity);

     String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";


            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);


            ImageView myImage = new ImageView(this);
            myImage.setImageBitmap(myBitmap);
            layout1.addView(myImage);

回答1:


do NOT access the SD card directly, try accessing it trough Environment. Like this:

String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";

and then you can call BitmapFactory:

Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);


来源:https://stackoverflow.com/questions/11004744/android-displaying-jpg-image-from-sdcard-using-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!