无法打开存储在SD卡图像文件 [英] Cannot open image file stored in sdcard
问题描述
在我的应用程序一个tableLayout的内容保存为一个文件夹中的图像。为了让用户从打开保存的图像,我创建了一个包含这些文件的名称的文本文件的文件。这些文件的名称将在后面的一个数组(文件)加载。用户点击打开以查看文件名的列表,并选择他想要打开之一。我用下面的code打开一个文件。
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);
和它显示叫MyFile的形象。
我已经检查,如果我得到正确的文件名和路径,这似乎是正确的。 (当我点击myFile.PNG在列表中,并显示我得到/mnt/sdcard/myFolder/myFile.PNG的路径)。
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").
为什么当我使用的第一个code不工作?
Why doesn't it work when I use the first code?
推荐答案
字符串连接不是路径结合起来的好方法。这是更好地使用文件构造:
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());
这篇关于无法打开存储在SD卡图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!