java - Take camera image and add to slider in android -


i'm new android , i'm trying add images taken camera , add them viewpager. it's supposed image carousel. i've tried far, problem same image being added 5 times. want know can change in getcount() method of customadapter in order different images carousel.

my mainactivity

public class mainactivity extends appcompatactivity {  public final static int capture_image_activity_request_code = 1034; public string photofilename = "photo.jpg"; public final string app_tag = "mycustomapp"; viewpager viewpager; customswipeadapter adapter;  imageview iv;   protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);    button btn = (button) findviewbyid(r.id.button1);   viewpager = (viewpager) findviewbyid(r.id.view_pager);     btn.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {intent intent = new intent(mediastore.action_image_capture);             intent.putextra(mediastore.extra_output, getphotofileuri(photofilename));              if (intent.resolveactivity(getpackagemanager()) != null) {                 startactivityforresult(intent, capture_image_activity_request_code);             }          }     });  }   //on activity result..   protected void onactivityresult(int requestcode, int resultcode, intent data) {if (requestcode == capture_image_activity_request_code) {         if (resultcode == result_ok) {               uri takenphotouri = getphotofileuri(photofilename);             bitmap takenimage = bitmapfactory.decodefile(takenphotouri.getpath());               adapter = new customswipeadapter(this, iv, takenimage);             viewpager.setadapter(adapter);          } else {             toast.maketext(this, "picture wasn't taken!", toast.length_short).show();         }     } }   public uri getphotofileuri(string filename) {     if (isexternalstorageavailable()) {         file mediastoredir = new file(getexternalfilesdir(environment.directory_pictures), app_tag);          if (!mediastoredir.exists() && !mediastoredir.mkdirs()) {             log.d(app_tag, "failed create directory");         }         return uri.fromfile(new file(mediastoredir.getpath() + file.separator + filename));      }     return null; }  private boolean isexternalstorageavailable() {     string state = environment.getexternalstoragestate();     return state.equals(environment.media_mounted); } 

custom adapter

public class customswipeadapter extends pageradapter { private context ctx; private layoutinflater layoutinflater; imageview iv; bitmap imagetaken;   public customswipeadapter(context ctx, imageview iv, bitmap imagetaken) {     this.ctx = ctx;     this.iv = iv;     this.imagetaken = imagetaken; }  @override public int getcount() {     return 5;  //need image count here... }  @override public boolean isviewfromobject(view view, object object) {      return (view == (linearlayout) object); }  @override public object instantiateitem(viewgroup container, int position) {     layoutinflater = (layoutinflater)      ctx.getsystemservice(context.layout_inflater_service);     view item_view = layoutinflater.inflate(r.layout.swipe_layout, container, false);     textview tv = (textview) item_view.findviewbyid(r.id.image_count);     iv = (imageview) item_view.findviewbyid(r.id.imageview1);     iv.setimagebitmap(imagetaken);     tv.settext("image" + position);     container.addview(item_view);     return item_view; }  @override public void destroyitem(viewgroup container, int position, object object) {      container.removeview((linearlayout) object);   } } 

activity_main.xml

    <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".mainactivity">      <android.support.v4.view.viewpager         android:id ="@+id/view_pager"         android:layout_height="match_parent"         android:layout_width="match_parent"         />      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true"         android:layout_centerhorizontal="true"         android:text="take photo"></button> </relativelayout> 

swipe_layout.xml

<?xml version="1.0" encoding="utf-8"?>   <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <textview         android:id="@+id/image_count"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="25dp"         android:gravity="center"         android:text="hello world"         android:textsize="25dp"         android:textstyle="bold" />      <imageview         android:id="@+id/imageview1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_above="@+id/button1"         android:layout_alignparenttop="true"         android:layout_margintop="25dp" /> </linearlayout> 

you seeing 1 image 5 times because supplying 1 bitmap adapter constructor , showing image. please add new image taken camera arraylist<bitmap> bitmaplist , pass bitmap adapter.

and in getview method take bitmap arralist , show in carousel.

hope you.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -