PDA

View Full Version : مشکل در ساخت یک گالری عکس



mohsenshahab
جمعه 07 خرداد 1395, 10:21 صبح
سلام . من یه مشکل دارم که خوشحال میشم کمکم کنید .
من یه اپ میخوام بنویسم که عکس های دوربین رو نشون بده و برای این کار هم از picasso استفاده کردم . ولی مشکلم اینه که توی امولیتور ارور میده . توی گوشیم که حافظه خارجی نداره درست کار میکنه و توی تبلت بعضی عکس ها رو نشون میده .

سورس برنامه


public class MainActivity extends Activity {

GridView gv;
public ArrayList<File> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

list = imageReader(Environment.getExternalStoragePublicDi rectory(Environment.DIRECTORY_PICTURES));

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

gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


startActivity(new Intent(getApplicationContext(),ViewImageActivity.c lass).putExtra("img",list.get(i).toString()));

}
});

}

class GridAdapter extends BaseAdapter{
public Context context = getApplicationContext();

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

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

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

if(view == null) {
view = getLayoutInflater().inflate(R.layout.single_grid, viewGroup, false);
}
ImageView iv = (ImageView) view.findViewById(R.id.imageView);

if(list.get(i).exists()){
Picasso.with(context)
.load(list.get(i))
.resize(150,150)
.centerCrop()
.into(iv);
}else{
Picasso.with(context).cancelRequest(iv);
}
return view;
}
}
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;
}





اروری که میده



Process: com.mohsen.gd, PID: 3195
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohsen.gd/com.mohsen.gd.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.jav a:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
atcom.mohsen.gd.MainActivity.imageReader(MainActiv ity.java:97)
at com.mohsen.gd.MainActivity.onCreate(MainActivity.j ava:31)
at android.app.Activity.performCreate(Activity.java:6 237)
at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.jav a:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:616)