Access videos both in internal storage and external SD card - Android -
i've developed simple android app, pull videos path="storage/microsd/videos" , present them in list view, upon selecting video, selected video played. videos located in storage/microsd/videos played. videos in other path not fetched. please give idea, how videos paths(internal storage , external storage). additionally if run same app on different phones, sd card path differs storage/sd card o/videos
. question: how can file
-object pointing default video-directory of device programmatically?
main activity
public class mainactivity extends activity { private listview mlistview; private list<string> filenamelist; public string path="storage/microsd/videos"; private file file; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); file = environment.getexternalstoragedirectory(); filenamelist = getfilelistfromsdcard(); final listview listview = (listview) findviewbyid(r.id.list); arrayadapter<string> adapter1 = new arrayadapter<string> (this,android.r.layout.simple_list_item_1, android.r.id.text1, filenamelist); listview.setadapter(adapter1); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { int itemposition = position; string itemvalue = (string) listview.getitematposition(position); intent intent = new intent(mainactivity.this, com.video.videolibrary.selectedvideo.class); intent.putextra("id", id); intent.putextra("itemvalue", itemvalue); intent.putextra("path", path); startactivity(intent); } }); button button = (button) findviewbyid(r.id.btn_online); // capture button clicks button.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { intent myintent = new intent(mainactivity.this,com.video.videolibrary.selectedvideo.class); startactivity(myintent); } }); } private list<string> getfilelistfromsdcard() { file files = new file(path); filefilter filter = new filefilter() { private final list<string> exts = arrays.aslist("mp4","mp4"); @override public boolean accept(file pathname) { string ext; string path = pathname.getpath(); ext = path.substring(path.lastindexof(".") + 1); return exts.contains(ext); } }; final file [] filesfound = files.listfiles(filter); list<string> fllst = new arraylist<string>(); if (filesfound != null && filesfound.length > 0) { (file file : filesfound) { fllst.add(file.getname()); } } return fllst; } }
selected video class
public class selectedvideo extends activity { videoview view;audiomanager audiomanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.selected_video); view = (videoview)findviewbyid(r.id.videoview1); bundle bundle = getintent().getextras(); string path=bundle.getstring("path"); string itemvalue = bundle.getstring("itemvalue"); view.setvideouri(uri.parse(path+"/"+itemvalue )); videoview videoview = (videoview) findviewbyid(r.id.videoview1); mediacontroller mediacontroller = new mediacontroller(this); mediacontroller.setanchorview(videoview); uri uri = uri.parse(path+"/"+itemvalue); displaymetrics dm = new displaymetrics(); this.getwindowmanager().getdefaultdisplay().getmetrics(dm); int height = dm.heightpixels; int width = dm.widthpixels; videoview.setminimumwidth(width); videoview.setminimumheight(height); videoview.setmediacontroller(mediacontroller); videoview.setvideouri(uri); videoview.requestfocus(); audiomanager = (audiomanager)getsystemservice(context.audio_service); int maxvolume = audiomanager.getstreammaxvolume(audiomanager.stream_music); int curvolume = audiomanager.getstreamvolume(audiomanager.stream_music); seekbar volcontrol = (seekbar)findviewbyid(r.id.volbar); volcontrol.setmax(maxvolume); volcontrol.setprogress(curvolume); volcontrol.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar arg0) { // todo auto-generated method stub } @override public void onstarttrackingtouch(seekbar arg0) { // todo auto-generated method stub } @override public void onprogresschanged(seekbar arg0, int arg1, boolean arg2) { // todo auto-generated method stub audiomanager.setstreamvolume(audiomanager.stream_music, arg1, 0); } }); button stopbutton = (button) findviewbyid(r.id.btn_stop); stopbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { view.pause(); } }); button playbutton = (button) findviewbyid(r.id.btn_play); playbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { bundle bundle = getintent().getextras(); string itemvalue = bundle.getstring("itemvalue"); toast.maketext(getapplicationcontext(), itemvalue, toast.length_long) .show(); view.start(); } }); button backbutton = (button) findviewbyid(r.id.btn_back); backbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { view.stopplayback(); startactivity(new intent(selectedvideo.this, mainactivity.class)); } }); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); } }
android manifest file
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <application android:allowbackup="true" android:icon="@mipmap/video_library" android:label="@string/app_name" android:theme="@style/apptheme" android:configchanges="orientation|keyboardhidden"> <activity android:name="com.video.videolibrary.mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".selectedvideo"> </activity> </application> </manifest>
first of all: don't hardcode path sdcard. should rather use getexternalstoragedirectory ()
file
-object pointing @ location. variable files
should assigned following:
file files= environment.getexternalstoragedirectory(environment.directory_movies);
as return default location, videos, accessable user stored, 1 should cover needs.
the android api says this:
note: don't confused word "external" here. directory can better thought media/shared storage. filesystem can hold relatively large amount of data , shared across applications (does not enforce permissions). traditionally sd card, may implemented built-in storage in device distinct protected internal storage , can mounted filesystem on computer.
another option use contentprovider
. cursor can query 1 , every indexed video. make explicit searching videos unneccessary.
a basic cursor
-query this:
cursor cursor=getcontentresolver().query( mediastore.video.media, // content uri of table mprojection, // columns return each row mselectionclause, // selection criteria null, // replacing placeholders of selection criterias msortorder); // sort order returned rows while(cursor.movetonext()){ //work data here }
delete line android-manifest.xml, it's unneccessary:
<uses-permission android:name="android.permission.read_external_storage" />
Comments
Post a Comment