0
我只是想壓縮兩個文件。有一個調用Compress類的MainActivity。看起來Compress.zip()方法中的循環正在循環,但在out.close()之前它會給出錯誤?這是因爲我沒有創建的zip文件?如果是的話,我該如何檢查?Java - Android - ZIP文件不斷帶回println需要消息錯誤
這裏是代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
}
}
String path = mediaStorageDir.getPath() + File.separator;
String zip = path+"images.zip";
String[] files = new String[3];
files[0] = path+"IMG_1.jpg";
files[1] = path+"IMG_2.jpg";
Compress comp = new Compress(files, zip);
comp.zip();
}
}
public class Compress {
private static final int BUFFER = 2048;
private String[] mfiles;
private String mzip;
public Compress(String[] files, String zip){
mfiles = files;
mzip = zip;
}
public void zip(){
try{
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(mzip);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
for(int i=0; i< mfiles.length;i++){
Log.v("Compress", mfiles[i]);
FileInputStream fi = new FileInputStream(mfiles[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(mfiles[i].substring(mfiles[i].lastIndexOf("/")+1));
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0, BUFFER)) != -1){
out.write(data, 0, count);
}
origin.close();
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
錯誤信息
09-19 08:41:40.911: V/Compress(5910): /storage/emulated/0/Pictures/MyCameraApp/IMG_1.jpg
09-19 08:41:41.282: V/Compress(5910): /storage/emulated/0/Pictures/MyCameraApp/IMG_2.jpg
09-19 08:41:41.602: W/System.err(5910): java.lang.NullPointerException: println needs a message
09-19 08:41:41.602: W/System.err(5910): at android.util.Log.println_native(Native Method)
09-19 08:41:41.602: W/System.err(5910): at android.util.Log.v(Log.java:119)
09-19 08:41:41.602: W/System.err(5910): at net.assemblyx.damage.Compress.zip(Compress.java:30)
09-19 08:41:41.602: W/System.err(5910): at net.assemblyx.damage.MainActivity.onCreate(MainActivity.java:38)
09-19 08:41:41.602: W/System.err(5910): at android.app.Activity.performCreate(Activity.java:5372)
09-19 08:41:41.602: W/System.err(5910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
09-19 08:41:41.602: W/System.err(5910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
09-19 08:41:41.602: W/System.err(5910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
09-19 08:41:41.602: W/System.err(5910): at android.app.ActivityThread.access$700(ActivityThread.java:165)
09-19 08:41:41.602: W/System.err(5910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
09-19 08:41:41.602: W/System.err(5910): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 08:41:41.602: W/System.err(5910): at android.os.Looper.loop(Looper.java:137)
09-19 08:41:41.602: W/System.err(5910): at android.app.ActivityThread.main(ActivityThread.java:5455)
09-19 08:41:41.602: W/System.err(5910): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 08:41:41.602: W/System.err(5910): at java.lang.reflect.Method.invoke(Method.java:525)
09-19 08:41:41.602: W/System.err(5910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
09-19 08:41:41.602: W/System.err(5910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
09-19 08:41:41.602: W/System.err(5910): at dalvik.system.NativeStart.main(Native Method)
謝謝,現在即時通訊到新的錯誤 – K3NN3TH 2014-09-19 14:54:12
你得到了什麼錯誤? – Blackbelt 2014-09-19 14:55:02
我有我的數組設置爲3而不是2,我有三個圖像,但刪除了一個,忘了調整數組。 – K3NN3TH 2014-09-19 14:58:32