0

In my application I save the contents of a tableLayout as an image in a folder. To allow user to open a file from the saved images I've created a text file that contains the names of these files. These file names will be loaded in an array (files) later. User clicks on "open" to see a list of file names and selects the one he wants to open. I'm using the following code to open a file.

final String imageInSD = extStorageDirectory+"/myFolder/"+files[which];
//where 'files' is an array of strings and contains the names of files.
//and 'which' is the index of the selected element in the list  
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);  
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);

when I try it nothing happens so I tried the following

final String imageInSD = extStorageDirectory+"/myFolder/myFile.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);  
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);

And it shows the image named myFile. I've already checked if I'm getting the correct file name and path and it seems to be correct. (when i click on myFile.PNG in the list and show the path I get "/mnt/sdcard/myFolder/myFile.PNG").

Why doesn't it work when I use the first code?

Anila
  • 1,136
  • 2
  • 18
  • 42

1 Answers1

1

String concatenation isn't a good way to combine paths. It is better to use the File constructor :

File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());
Community
  • 1
  • 1
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • Well your problem is elsewhere because your code is correct now. Be sure you don't have extra spaces or another unwanted characters in `files[which]` (like new line characters). – Dalmas Dec 19 '11 at 16:05
  • IT WORKS! I can't believe i spent 2 hours looking for the prob when it was so obvious. I had "\n" in my file names :s. Thanks a lot once again. – Anila Dec 19 '11 at 16:10