无法打开存储在 SD 卡中的图像文件
在我的应用程序中,我将 tableLayout 的内容保存为文件夹中的图像。为了允许用户从保存的图像中打开文件,我创建了一个包含这些文件名称的文本文件。这些文件名稍后将加载到数组(文件)中。用户单击“打开”查看文件名列表并选择他想要打开的文件。我正在使用以下代码打开文件。
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);
当我尝试时没有任何反应,所以我尝试了以下操作
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”)。
为什么我使用第一个代码时不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串连接不是组合路径的好方法。最好使用 文件构造函数 :
String concatenation isn't a good way to combine paths. It is better to use the File constructor :