Sun
Sun

Reputation: 6888

Null Pointer Exception SD Card Folder has no Photo

How to show Toast if list has no Photo, i am making an app in which allowing user to take pic and storing that pic into specific folder, which created automatically,

Whenever I do click on View button, if user has taken screens then its showing me those photos in a List, and if user has not taken any pic then getting Exception, so here i want to show a Toast Message if user has not captured any pic.

Logcat:

07-22 06:48:28.248: E/AndroidRuntime(841): FATAL EXCEPTION: main
07-22 06:48:28.248: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.UploadActivity}: java.lang.NullPointerException
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.os.Looper.loop(Looper.java:137)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 06:48:28.248: E/AndroidRuntime(841):  at java.lang.reflect.Method.invokeNative(Native Method)
07-22 06:48:28.248: E/AndroidRuntime(841):  at java.lang.reflect.Method.invoke(Method.java:511)
07-22 06:48:28.248: E/AndroidRuntime(841):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 06:48:28.248: E/AndroidRuntime(841):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 06:48:28.248: E/AndroidRuntime(841):  at dalvik.system.NativeStart.main(Native Method)
07-22 06:48:28.248: E/AndroidRuntime(841): Caused by: java.lang.NullPointerException
07-22 06:48:28.248: E/AndroidRuntime(841):  at com.example.UploadActivity.getSD(UploadActivity.java:125)
07-22 06:48:28.248: E/AndroidRuntime(841):  at com.example.UploadActivity.onCreate(UploadActivity.java:106)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.Activity.performCreate(Activity.java:5104)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 06:48:28.248: E/AndroidRuntime(841):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-22 06:48:28.248: E/AndroidRuntime(841):  ... 11 more

SingleAngelActivity.java:

  Button captureButton = (Button) findViewById(R.id.btnCapture);
    Log.d(SingleAngelActivity.LOG_TAG, "captureButton :: " + captureButton);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
            Log.d(SingleAngelActivity.LOG_TAG, "mCamera.takePicture :: " + mCamera);                                            
        }
    });

    Button viewButton = (Button) findViewById(R.id.btnView);
    Log.d(SingleAngelActivity.LOG_TAG, "SingleAngelActivityButton :: " + viewButton);
    viewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {  
            Intent intentNewEvent = new Intent(SingleAngelActivity.this, UploadActivity.class);
            startActivity(intentNewEvent);                  
        }
    });

UploadActivity.java:

private List <String> getSD()
        {
            List <String> it = new ArrayList <String>();

            String string = "/mnt/sdcard/Pictures/MyNewImages/";
            File f = new File (string+ SingleAngelActivity.customFolder+ "/");

            Log.d(UploadActivity.LOG_TAG, "KEY_TITLE :::--- " + f);
            File[] files = f.listFiles ();
            Log.d(UploadActivity.LOG_TAG, "getListImages() :::--- " + files);

            for (int i = 0; i <files.length; i++)           
            {
                File  file = files[i];
                Log.d(UploadActivity.LOG_TAG, "i<files.length() :::--- " + i);
                Log.d("Count",file.getPath());
                it.add (file.getPath());
            }
            Log.d(UploadActivity.LOG_TAG, "List <String> it :::--- " + it);
            return it;
        }

Upvotes: 0

Views: 401

Answers (1)

Anoop
Anoop

Reputation: 993

It works only if the SD card is mounted, the path is correct and no other problems. It just checks whether the folder have any files or not

try the following code

private List <String> getSD()
    {
        List <String> it = new ArrayList <String>();

        String string = "/mnt/sdcard/Pictures/MyNewImages/";
        File f = new File (string+ SingleAngelActivity.customFolder+ "/");

        Log.d(UploadActivity.LOG_TAG, "KEY_TITLE :::--- " + f);
        File[] files = f.listFiles ();
        Log.d(UploadActivity.LOG_TAG, "getListImages() :::--- " + files);
        if(files == null)
         Toast.MakeText(this,"No photo",700).show();
        else
        for (int i = 0; i <files.length; i++)           
        {
            File  file = files[i];
            Log.d(UploadActivity.LOG_TAG, "i<files.length() :::--- " + i);
            Log.d("Count",file.getPath());
            it.add (file.getPath());
        }
        Log.d(UploadActivity.LOG_TAG, "List <String> it :::--- " + it);
        return it;
    }

Upvotes: 2

Related Questions