How to add a dynamic images in ViewFlipper ???
I am using this code. to access the particular folder in order to get a list of images and add it into a ViewFlipper
. but i have problem with this, i can't add it into the ViewFlipper.So please if any one know how to resolve this problem, i will appreciate his help . Sorry for my bad english.
public ImageView[] imageView;
byte[] mByte = null;
public BufferedInputStream mBufferedInputStream;
Bitmap[] bMap;
File dir=new File("/sdcard/image folder");
System.out.println("Dir is"+dir);
String[] file_name = dir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
if(new File(dir,name).isDirectory())
return true;
return name.toLowerCase().endsWith(".jpg");
}
});
for(String singleFile : file_name){
System.out.println("---------->"+dir.toString()+"/"+singleFile);
try {
FileInputStream mFileInputStream = new FileInputStream(dir+"/"+singleFile);
mBufferedInputStream = new BufferedInputStream(mFileInputStream);
mByte = new byte[mBufferedInputStream.available()+(16*1024)];
mBufferedInputStream.read(mByte);
bMap = new Bitmap[file_name.length];
bMap[position] = BitmapFactory.decodeByteArray(mByte, 0, mByte.length);
imageView[file_name.length] = new ImageView(this);
imageView[position].setImageBitmap(bMap[position]);
mViewFlipper.addView(imageView[position]);
bMap[position].recycle();
position++;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mViewFlipper.startFlipping();
mViewFlipper.setOnTouchListener(this);
In the LogCat :
09-27 19:27:17.490: WARN/System.err(9910): java.lang.NullPointerException
09-27 19:27:17.490: WARN/System.err(9910): at com.android.SD_Card_Img_Reader.SD_CardImageReaderActivity.on开发者_开发技巧Create(SD_CardImageReaderActivity.java:76)
09-27 19:27:17.501: WARN/System.err(9910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-27 19:27:17.501: WARN/System.err(9910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-27 19:27:17.501: WARN/System.err(9910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-27 19:27:17.511: WARN/System.err(9910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-27 19:27:17.511: WARN/System.err(9910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-27 19:27:17.541: WARN/System.err(9910): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 19:27:17.584: WARN/System.err(9910): at android.os.Looper.loop(Looper.java:123)
09-27 19:27:17.584: WARN/System.err(9910): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-27 19:27:17.584: WARN/System.err(9910): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 19:27:17.584: WARN/System.err(9910): at java.lang.reflect.Method.invoke(Method.java:521)
09-27 19:27:17.584: WARN/System.err(9910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-27 19:27:17.584: WARN/System.err(9910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-27 19:27:17.590: WARN/System.err(9910): at dalvik.system.NativeStart.main(Native Method)
09-27 19:27:17.590: INFO/System.out(9910): ---------->/sdcard/image folder/darkred.jpg
09-27 19:27:17.590: INFO/global(9910): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
09-27 19:27:17.721: DEBUG/dalvikvm(9910): GC_EXTERNAL_ALLOC freed 406 objects / 125952 bytes in 125ms
09-27 19:27:18.571: WARN/System.err(9910): java.lang.NullPointerException
09-27 19:27:18.651: WARN/System.err(9910): at com.android.SD_Card_Img_Reader.SD_CardImageReaderActivity.onCreate(SD_CardImageReaderActivity.java:76)
09-27 19:27:18.651: WARN/System.err(9910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-27 19:27:18.771: WARN/System.err(9910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-27 19:27:18.771: WARN/System.err(9910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-27 19:27:18.771: WARN/System.err(9910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-27 19:27:18.771: WARN/System.err(9910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-27 19:27:18.771: WARN/System.err(9910): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 19:27:18.771: WARN/System.err(9910): at android.os.Looper.loop(Looper.java:123)
09-27 19:27:18.771: WARN/System.err(9910): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-27 19:27:18.771: WARN/System.err(9910): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 19:27:18.771: WARN/System.err(9910): at java.lang.reflect.Method.invoke(Method.java:521)
09-27 19:27:18.771: WARN/System.err(9910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-27 19:27:18.771: WARN/System.err(9910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-27 19:27:18.771: WARN/System.err(9910): at dalvik.system.NativeStart.main(Native Method)
ViewFlipper only allows you to have one child per view. Inside that child you can add as many images as you want. You might want to add a LinearLayout and start adding images inside of that.
精彩评论