【发布时间】:2018-04-14 06:29:15
【问题描述】:
我为学校制作了这个应用程序,明天需要提交。小问题,它总是在某个点崩溃。
我的应用从 Firebase 下载一个 .zip 文件并将其解压缩:
-- 缓存目录
--images
--image01.jpg
--image02.jpg
--image03.jpg
--and so on...
--info.txt
--scan234702640.zip // this is the file that includes the /images and the info.txt
所以我的类 imageHandler 中有以下代码:
// Get the file
gsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
String path = "";
File f = new File(context.getExternalCacheDir().toURI());
File[] files1 = f.listFiles();
for (File inFile1 : files1) {
if (inFile1.getName().contains("scan")) {
path = context.getExternalCacheDir() + "/" + inFile1.getName();
}
}
//Decompress zip and build bitmap array
try {
ZipFile zip = new ZipFile(path);
zip.extractAll(context.getExternalCacheDir() + "/");
} catch (ZipException e){
Toast.makeText(context, "ZIP error " + e.getCode() + " : " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
//Construct bitmap array
ArrayList<Bitmap> pictures = new ArrayList<Bitmap>();
File f3 = new File(context.getExternalCacheDir().toString() + "/images/");
if (f3.exists()) {
File[] files3 = f3.listFiles(); //THE PROBLEM IS SOMEWHERE HERE
if (files3 != null) {
for (File inFile3 : files3) {
if (inFile3.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(inFile3.getPath(), options);
pictures.add(bitmap);
}
}
}
}
// DEBUG: Show what we have in our cache location
String message = "";
File f2 = new File(context.getExternalCacheDir().toURI() + "/images");
File[] files2 = f2.listFiles();
for (File inFile2 : files2) {
if (inFile2.isDirectory()) {
message += "/" + inFile2.getName() + "\n";
} else {
message += inFile2.getName() + "\n";
}
}
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle("Test Message")
.setMessage(message)
.show();
// END OF DEBUG
}
});
return scan;
因此,它在显示“问题出在此处”的部分崩溃。我已经询问了所有 EXTERNAL_STORAGE 权限,并检查了文件是否存在。我发现,在调试模式下,它成功地将位图添加到数组中,但是当我有例如 10 个图像时,它在加载所有 10 个图像后崩溃。 所以请帮助我;)
非常感谢, 朱尔斯
编辑 这是控制台日志:
10-31 00:07:31.060 7536-7536/com.julescitronic.meeting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.julescitronic.meeting, PID: 7536
java.lang.NullPointerException: Attempt to get length of null array
at com.julescitronic.meeting.imageHandler$2.onSuccess(imageHandler.java:152)
at com.julescitronic.meeting.imageHandler$2.onSuccess(imageHandler.java:127)
at com.google.firebase.storage.StorageTask$1.zza(Unknown Source:9)
at com.google.firebase.storage.StorageTask$1.zzl(Unknown Source:4)
at com.google.firebase.storage.zze$2.run(Unknown Source:10)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
【问题讨论】:
-
images/image? -
哦,对不起。正确的是图像。我刚才打错了。然而,这甚至不可能是问题。图像可以很好地加载到位图数组中。我只是在它这样做后崩溃了
-
是的,我看到您也检查了它,但要正确。
-
如果您发布正在发生的特定错误,可能会更容易诊断。
-
在您的最后一句话中,您似乎更像是内存问题而不是权限问题。您能否提供更多信息或日志?
标签: java android arrays for-loop nullpointerexception