K3NN3TH
K3NN3TH

Reputation: 1496

Java - Android - ZIP files keeps bring back println needs a message error

I'm just trying to compress two files. Have a MainActivity that calls a Compress class. It seems that the loop in the Compress.zip() method is looping but right before the out.close() it gives an error? Is this because I do not have the zip file created already? If so how do I check for that?

Here is Codes:

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();
        }
    }
}

ERROR MESSAGE

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)

Upvotes: 0

Views: 59

Answers (1)

Blackbelt
Blackbelt

Reputation: 157487

as the stacktrace says, println need to have a message.

  Log.v("Compress", mfiles[i]);

if mfiles[i] is null, it will make your app crash with that message. Change it with

 Log.v("Compress", mfiles[i] != null ? mfiles[i] : " mfiles at i is null");

Upvotes: 1

Related Questions