0
FATAL EXCEPTION: main
                                                         Process: a2day.a2day, PID: 28475
                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{a2day.a2day/a2day.a2day.Explore}: java.lang.NullPointerException: Attempt to get length of null array
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:154)
                                                             at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                          Caused by: java.lang.NullPointerException: Attempt to get length of null array
                                                             at a2day.a2day.Explore.imageReader(Explore.java:75)
                                                             at a2day.a2day.Explore.onCreate(Explore.java:28)
                                                             at android.app.Activity.performCreate(Activity.java:6912)
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:154) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:6688) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

Above is the errors I'm approaching when trying to load images from my directory... below I am adding the java classes that I have to try get the images to load on the android device.

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class ViewImage extends AppCompatActivity {

ImageView iv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_image);

    String f = getIntent().getStringExtra("img");

    iv2 = (ImageView) findViewById(R.id.imageView2);
    iv2.setImageURI(Uri.parse(f));
}

}

public class Explore extends ActionBarActivity {

GridView gv;
ArrayList<File> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_explore);

    list = imageReader(Environment.getExternalStorageDirectory());

    gv = (GridView) findViewById(R.id.gridView);
    gv.setAdapter(new GridAdapter());

    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img", list.get(position)));
        }
    });

}

private class GridAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = getLayoutInflater().inflate(R.layout.my_grid, parent ,false);
        ImageView iv = (ImageView) convertView.findViewById(R.id.singleGrid);

        iv.setImageURI(Uri.parse(getItem(position).toString()));

        return convertView;
    }
}

ArrayList<File> imageReader(File root){
    ArrayList<File> a = new ArrayList<>();

    File[] files = root.listFiles();
    for(int i =0; i < files.length; i++) {
        if(files[i].isDirectory()) {
            a.addAll(imageReader(files[i]));
        } else {
            if(files[i].getName().endsWith(".jpg")) {
                a.add(files[i]);
            }
        }
    }
    return a;
}

}

cans someone help me fix this code please? I have no idea what is wrong with it and its stressing me out so much as I have little time to get this done as I have other pages needed to be done.

  • Make sure your have the `READ_EXTERNAL_STORAGE` permission (or `WRITE_EXTERNAL_STORAGE`) in the manifest, and that you're handling it appropriately If you're running on Marshmallow and above: http://stackoverflow.com/questions/32635704. – Mike M. Apr 30 '17 at 19:21
  • I have got both in my manifest @MikeM. I don't get why I'm getting these errors any chance of fixing it maybe? I don't understand why its not working – samsmith305 Apr 30 '17 at 19:26
  • Did you read through the post I linked above? – Mike M. Apr 30 '17 at 19:33
  • @MikeM Ive dropped it down to sdk 22 as it says dropping the sdk on that link and still doesn't work? Caused by: java.lang.NullPointerException: Attempt to get length of null array – samsmith305 Apr 30 '17 at 19:37
  • when do I add a check permission on my code? – samsmith305 Apr 30 '17 at 19:39
  • If you're dropping the `targetSdkVersion`, make sure you do it in the build.gradle file, not the manifest. You should also clean/rebuild, and uninstall/reinstall after changing it. If you're doing the runtime check and request, then it needs to be done before before you try to access the external files. – Mike M. Apr 30 '17 at 19:46
  • yeah I changed it in the Build.gradle file, cleaned the project and still the same error as shown above @MikeM – samsmith305 Apr 30 '17 at 19:49
  • Did you uninstall/reinstall the app? Are you sure you have the permissions in the right spot in the manifest? Outside of the `` tags? – Mike M. Apr 30 '17 at 19:56
  • yes I've done that and also, it is outside the tag.. thats why I'm confused its not working? – samsmith305 Apr 30 '17 at 19:59
  • Are the permissions spelled exactly correctly? E.g., ``. Does the device have any additional settings or permissions beyond the standard Android ones that prevent third-party apps from accessing that by default? – Mike M. Apr 30 '17 at 20:03
  • where would I find them ? if its on phone setting then shouldn't do as I used this feature before to access this feature on a different app I made – samsmith305 Apr 30 '17 at 20:08
  • I'm not sure what you're saying. I wouldn't know where to look for those additional settings/permissions. I just know that some manufacturers have similar restrictions for some things; e.g., accessing SMS, running at boot, etc. If you've done this before on the same device, then that would kinda rule out the additional settings/permissions, though. In that case, there must be something you're missing in the current project. – Mike M. Apr 30 '17 at 20:17
  • all I'm trying to do is get my images from my phone onto a grid view to show inside the application – samsmith305 Apr 30 '17 at 20:27

0 Answers0