Cannot open image file stored on SD card

In my application, I save the contents of the Layout table as an image in a folder. To allow the user to open a file from saved images, I created a text file containing the names of these files. These file names will be loaded into the array (files) later. The user clicks the open button to view a list of file names and select the one he wants to open. I use 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, 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 an image called myFile. I already checked if I get the correct file name and path, and that seems right. (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?

0
source share
1 answer

String concatenation is not a good way to combine paths. Better to use the constructor:

 File directory = new File(extStorageDirectory, "myFolder"); File fileInDirectory = new File(directory, files[which]); Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath()); 
+1
source

All Articles