• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps read image from external file

kineret

Lurker
hi,
I try to read picture from file that save in my computer.
the picture store in this path : C:\Users\naor\Desktop\project\more\home.jpg
and I wrote this code in my application
File imgFile = new File("\ C:\Users\naor\Desktop\project\more\home.jpg");
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.view1);
myImage.setVisibility(1);
myImage.setImageBitmap(myBitmap);
}
and when the program running it's not get inside to the if (these mean that imgFile.exists() is false)
I think that the problem is my path maybe it's not write good.
(I tried with / )

please your help
thanks
 
File imgFile = new File("\ C:\Users\naor\Desktop\project\more\home.jpg");

I am assuming you are using an emulator. The emulator cannot access files on your computer. You need to give it an sdcard in the AVD configuration and put the file on the virtual sd card.

More preferebly would be to put put it in your drawable, raw, or assets folders then load it accordingly. This way, you know that the image will always exist as it will be distributed with the application.
 
You can place the file in your /res/raw project folder. Then you can load and display it as follows:

Code:
Bitmap myBitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.raw.name_of_file_without_extension));
ImageView myImage = (ImageView) findViewById(R.id.view1);
             myImage.setImageBitmap(myBitmap);
 
Back
Top Bottom