如何在android中直接从.zip文件读取文件而不解压它

发布于 2024-12-23 04:40:35 字数 1438 浏览 1 评论 0原文

过去几个月我一直在研究 android,现在我的问题是读取放在 sdcard 上的 .zip 文件。我已经成功完成了将 .zip 文件下载到 SD 卡上的编码。

我已将 img.zip 文件下载到 SD 卡上。此 img.zip 包含 5 个图像文件。现在我可以不用解压 img.zip 直接读取它的内容......???如果是的话请帮忙。我在互联网上看到了一些例子,但他们都说先解压缩然后使用,我想避免这部分,因为我只是想为图像视图设置图像。

 ImageView imv = new ImageView(this);
 imv.setImageURI(Uri.parse("//sdcard/1.png"));

这就像下载单个图像并设置实际有效的 imv 源一样。现在我想要的是如下所示的东西。

 imv.setImageURI(Uri.parse("//sdcard/img.zip/1.png"));

我尝试过这个,但在我的布局中我看不到图像。

它可以完成吗...请帮助...

我通过以下代码使它工作....

 try { 
                 Bitmap mBackground=null;
                    FileInputStream fis = new FileInputStream("//sdcard/tp.zip"); 
                    ZipInputStream zis = new ZipInputStream(fis); 
                    ZipEntry ze = null; 
                    while ((ze = zis.getNextEntry()) != null) { 
                        if (ze.getName().equals("1.png")) {
                            Toast.makeText(con, "Found", 2).show();
                            mBackground = BitmapFactory.decodeStream(zis);
                            imv.setImageBitmap(mBackground);
                            break; 
                        } 
                    } 
                } catch (FileNotFoundException e) { 
                    e.printStackTrace(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 

I have been working on android for the past few months, now the problem for me is to read a .zip file placed on sdcard. I have successfully done the coding for downloading the .zip file on the sdcard.

I have img.zip file downloaded on to the sdcard. This img.zip contains 5 image files. Now instead of unzipping the img.zip can i directly read its content...??? if yes plz help. I saw few examples over internet but they all say to unzip and then use, i want to avoid that part because i simply want to set the images for an imageview.

 ImageView imv = new ImageView(this);
 imv.setImageURI(Uri.parse("//sdcard/1.png"));

this is like downloading a single image and setting the source of imv which actually works. Now what i want is something as shown below.

 imv.setImageURI(Uri.parse("//sdcard/img.zip/1.png"));

I tried this, but in my layout i don't see the images.

can it be done... plz help...

I got it working by the following code....

 try { 
                 Bitmap mBackground=null;
                    FileInputStream fis = new FileInputStream("//sdcard/tp.zip"); 
                    ZipInputStream zis = new ZipInputStream(fis); 
                    ZipEntry ze = null; 
                    while ((ze = zis.getNextEntry()) != null) { 
                        if (ze.getName().equals("1.png")) {
                            Toast.makeText(con, "Found", 2).show();
                            mBackground = BitmapFactory.decodeStream(zis);
                            imv.setImageBitmap(mBackground);
                            break; 
                        } 
                    } 
                } catch (FileNotFoundException e) { 
                    e.printStackTrace(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴梦长久 2024-12-30 04:40:35

尝试

 imv.setImageURI(Uri.parse("//sdcard/img.zip!/1.png"));

Try

 imv.setImageURI(Uri.parse("//sdcard/img.zip!/1.png"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文