如何在android中直接从.zip文件读取文件而不解压它
过去几个月我一直在研究 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try