0

i m trying to take image file to android storage but android event log show me error java.lang.RuntimeException and java.lang.nullpointerexception .

main java class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gridView=(GridView)findViewById(R.id.gridView);
    custom adCustom= new custom(this);
    gridView.setAdapter(adCustom);

}

custom.java

class custom extends BaseAdapter{

Context context;

public custom(Context context){
    this.context=context;

}

@Override
public int getCount(){
    return getImage().length;

}

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

@Override
public Object getItem(int position) {
    return null;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ImageView imageView = null;
    if(convertView==null){ imageView=new ImageView(context);}else{imageView=(ImageView)convertView;}

    imageView.setImageURI(getImage()[position]);
    return imageView;
}

Uri[] uri;
String[] mfile=null;
public Uri[] getImage(){
    File file= Environment.getExternalStorageDirectory();
    final File[] filelist=file.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return ((filename.endsWith(".jpg"))||(filename.endsWith(".png")));

        }
    }) ;  mfile=new String[filelist.length];
    for(int i=0;i<filelist.length;i++){
        mfile[i]=filelist[i].getAbsolutePath();
    }
    uri=new Uri[mfile.length];
    for(int i=0;i<uri.length;i++){
        uri[i]=Uri.parse(mfile[i]);
    }return uri;}}
andro
  • 11
  • 2
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Artjom B. Feb 09 '15 at 12:35

1 Answers1

0

You are re-creating and rebuilding the uri array every time getView is called. This is inefficient and can cause concurrency problems. So call getImage() only once, in the constructor of your adapter:

public custom(Context context){
    this.context=context;
    getImage();
}

Then you can access the images in your getView implementation:

imageView.setImageURI(uri[position]);

P.S. Name your adapter class something other than custom. How about CustomImageAdapter ?

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • class custom extends BaseAdapter { Context context; public custom(Context context) { this.context=context; getImage(); } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView ; if(convertView==null){ imageView=new ImageView(context); }else{imageView=(ImageView)convertView; } imageView.setImageURI(uri[position]); return imageView; } – andro Feb 09 '15 at 19:00
  • Uri[] uri; String[] mfile=null; public Uri[] getImage(){ File file= Environment.getExternalStorageDirectory(); final File[] filelist=file.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { return ((filename.endsWith(".jpg"))||(filename.endsWith(".png"))); } }) ; – andro Feb 09 '15 at 19:08
  • mfile=new String[filelist.length]; for(int i=0;i – andro Feb 09 '15 at 19:09
  • Uhhh probably should post it in your question. Edit update your question – Greg Ennis Feb 09 '15 at 19:16