无法打开存储在 SD 卡中的图像文件

发布于 2024-12-21 17:26:39 字数 933 浏览 0 评论 0原文

在我的应用程序中,我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-12-28 17:26:39

字符串连接不是组合路径的好方法。最好使用 文件构造函数

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文