android - Previewing multiple image and videos in Single Activity -
android - Previewing multiple image and videos in Single Activity -
i trying display 2 imageview in single actvity.these imageviews consist of frame of video.when clicked there corresponding video start playing.but problem when capture first video , homecoming main actvity no image displayed.when capture sec video , homecoming main actvity image preview of first video displayed.and when click on black screen displayed, when using single videoview , single imageview single video working fine. using fileuri , fileuri1 store path of videos. there problem in layout or programming error or positioning of views?
try way,hope help solve problem. <linearlayout 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" > <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <!-- capture image button --> <button android:id="@+id/btncapturepicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="take picture" android:layout_marginbottom="10dp"/> <!-- record video button --> <button android:id="@+id/btnrecordvideo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="record video" android:layout_marginbottom="10dp"/> <button android:id="@+id/see" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="see video" /> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:padding="10dp"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="preview" android:padding="10dp" android:textsize="15dp"/> <!-- display image taken --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_margintop="10dp" android:layout_marginbottom="10dp"> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginbottom="10dp"> <imageview android:id="@+id/imgpreview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustviewbounds="true" android:visibility="gone" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <imageview android:id="@+id/imgpreview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustviewbounds="true" android:visibility="gone"/> </linearlayout> </linearlayout> <!-- preview video recorded --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_margintop="10dp"> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginbottom="10dp"> <videoview android:id="@+id/videopreview" android:layout_width="wrap_content" android:layout_height="match_parent" android:visibility="gone"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <videoview android:id="@+id/videoview2" android:layout_width="wrap_content" android:layout_height="match_parent" android:visibility="gone"/> </linearlayout> </linearlayout> </linearlayout> </linearlayout> public class mainactivity extends activity { private static final int camera_capture_video_request_code = 200; public static final int media_type_video = 2; // directory name store captured images , videos private static final string image_directory_name = "hello camera"; public static uri fileuri=null; public static uri fileuri1=null;// file url store image/video private imageview imgpreview,imgpreview2; private videoview videopreview,videopreview2; private button btncapturepicture, btnrecordvideo,btnsee; private int countclick; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imgpreview = (imageview) findviewbyid(r.id.imgpreview); imgpreview2 = (imageview) findviewbyid(r.id.imgpreview2); videopreview = (videoview) findviewbyid(r.id.videopreview); videopreview2 = (videoview) findviewbyid(r.id.videoview2); btnrecordvideo = (button) findviewbyid(r.id.btnrecordvideo); btnsee = (button) findviewbyid(r.id.see); countclick=0; btnrecordvideo.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // record video countclick++; recordvideo(); } }); btnsee.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // capture image intent i= new intent(); //uri uri = i.getparcelableextra("fileuri"); i.putextra ("fileuri",fileuri); //i.putextra ("fileuri1",fileuri1); i.setclass(getapplicationcontext(), secondactivity.class); startactivity(i); } }); // checking photographic camera availability if (!isdevicesupportcamera()) { toast.maketext(getapplicationcontext(), "sorry! device doesn't back upwards camera", toast.length_long).show(); // close app if device does't have photographic camera finish(); } } /** * checking device has photographic camera hardware or not * */ private boolean isdevicesupportcamera() { if (getapplicationcontext().getpackagemanager().hassystemfeature( packagemanager.feature_camera)) { // device has photographic camera homecoming true; } else { // no photographic camera on device homecoming false; } } /* * here store file url null after returning photographic camera * app */ @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // save file url in bundle null on scren orientation // changes outstate.putparcelable("file_uri", fileuri); //outstate.putparcelable("file_uri", fileuri1); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); // file url fileuri = savedinstancestate.getparcelable("file_uri"); //fileuri1 = savedinstancestate.getparcelable("file_uri1"); } /* * recording video */ private void recordvideo() { intent intent = new intent(mediastore.action_video_capture); if(countclick%2==0){ fileuri1 = getoutputmediafileuri(media_type_video); // set video quality intent.putextra(mediastore.extra_video_quality, 1); intent.putextra(mediastore.extra_output, fileuri1); // set image file // name intent.putextra(mediastore.extra_duration_limit, 20); // start video capture intent startactivityforresult(intent, camera_capture_video_request_code); }else{ fileuri = getoutputmediafileuri(media_type_video); // set video quality intent.putextra(mediastore.extra_video_quality, 1); intent.putextra(mediastore.extra_output, fileuri); // set image file // name intent.putextra(mediastore.extra_duration_limit, 20); // start video capture intent startactivityforresult(intent, camera_capture_video_request_code); } } /** * receiving activity result method called after closing photographic camera * */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // if result capturing image if (requestcode == camera_capture_video_request_code) { if (resultcode == result_ok) { // video recorded // preview recorded video previewvideo(); } else if (resultcode == result_canceled) { // user cancelled recording toast.maketext(getapplicationcontext(), "user cancelled video recording", toast.length_short) .show(); } else { // failed record video toast.maketext(getapplicationcontext(), "sorry! failed record video", toast.length_short) .show(); } } } /* * previewing recorded video */ private void previewvideo() { seek { // hide image preview //if(fileuri==null) //{ /*imgpreview.setvisibility(view.gone); videopreview.setvisibility(view.visible); videopreview.setvideopath(fileuri.getpath()); // start playing videopreview.start();*/ /*videopreview.setvisibility(view.gone); imgpreview.setvisibility(view.visible); bitmap bmthumbnail; bmthumbnail = thumbnailutils.createvideothumbnail(fileuri.getpath() , thumbnails.micro_kind); imgpreview.setimagebitmap(bmthumbnail);*/ videopreview.setvisibility(view.gone); videopreview2.setvisibility(view.gone); //both videoviews made invisible , both imageviews made visible if(countclick>=2){ imgpreview.setvisibility(view.visible); imgpreview2.setvisibility(view.visible); mediametadataretriever retriever = new mediametadataretriever(); mediametadataretriever retriever1 = new mediametadataretriever(); retriever.setdatasource(fileuri.getpath()); retriever1.setdatasource(fileuri1.getpath()); bitmap mybitmap = retriever.getframeattime(1000000); bitmap mybitmap1 = retriever1.getframeattime(1000000); imgpreview.setimagebitmap(mybitmap); imgpreview2.setimagebitmap(mybitmap1); }else{ imgpreview.setvisibility(view.visible); mediametadataretriever retriever = new mediametadataretriever(); retriever.setdatasource(fileuri.getpath()); bitmap mybitmap = retriever.getframeattime(1000000); imgpreview.setimagebitmap(mybitmap); } imgpreview.setonclicklistener(new view.onclicklistener() { //clicking on first imageview @override public void onclick(view v) { imgpreview.setvisibility(view.gone); imgpreview2.setvisibility(view.gone); videopreview.setvisibility(view.visible); //first videoview video starts playing videopreview2.setvisibility(view.gone); videopreview.setvideopath(fileuri.getpath()); videopreview.start(); } }); imgpreview2.setonclicklistener(new view.onclicklistener() { //clicking on secong imageview @override public void onclick(view v) { imgpreview2.setvisibility(view.gone); imgpreview.setvisibility(view.gone); videopreview2.setvisibility(view.visible); //second videoview video starts playing videopreview.setvisibility(view.gone); videopreview2.setvideopath(fileuri1.getpath()); videopreview2.start(); } }); } grab (exception e) { e.printstacktrace(); } } /** * ------------ helper methods ---------------------- * */ /* * creating file uri store image/video */ public uri getoutputmediafileuri(int type) { homecoming uri.fromfile(getoutputmediafile(type)); } /* * returning image / video */ private static file getoutputmediafile(int type) { // external sdcard location file mediastoragedir = new file( environment .getexternalstoragepublicdirectory(environment.directory_pictures), image_directory_name); // create storage directory if not exist if (!mediastoragedir.exists()) { if (!mediastoragedir.mkdirs()) { log.d(image_directory_name, "oops! failed create " + image_directory_name + " directory"); homecoming null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); file mediafile; /*if (type == media_type_image) { mediafile = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg"); } else*/ if (type == media_type_video) { mediafile = new file(mediastoragedir.getpath() + file.separator + "vid_" + timestamp + ".mp4"); } else { homecoming null; } homecoming mediafile; } }
android video imageview videoview
Comments
Post a Comment