Example of an activity in Android that responds to an external action (you can use this to import external files)

Source: Internet
Author: User

If we click on a file in the file browser and then click on it, a list will pop up for you to choose



If you make your application appear on this list, you must add it to the Menifest activity:

        <activity android:name= "com.example.clickabletextview.MainActivity" android:label= "@strin G/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&gt                ; <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> <intent -filter> <action android:name= "Android.intent.action.VIEW"/> <action android:na                Me= "Android.intent.action.EDIT"/> <category android:name= "Android.intent.category.DEFAULT"/> <category android:name= "Android.intent.category.BROWSABLE"/> <data android:mimetype= " Application/pdf "/> <data android:mimetype=" image/bmp "/> <data android:mimetype = "Image/tiff"/> <data android:mimetype= "image/gif"/> <data android:mimetype= "I Mage/jpEg "/> <data android:mimetype=" image/x-ms-bmp "/> <data android:mimetype=" image/ PNG "/> <data android:mimetype=" image/x-pcx "/> <data android:mimetype=" Image/ta RGA "/> <data android:mimetype=" Image/x-photoshop "/> <data android:mimetype=" Te Xt/plain "/> <data android:mimetype=" Text/xml "/> <data android:mimetype=" text/h Tml "/> </intent-filter> <intent-filter> <action android:name=" Android                Oid.intent.action.VIEW "/> <category android:name=" Android.intent.category.DEFAULT "/> <data android:host= "*" android:mimetype= "*/*" Android:path Pattern= ". *\\.pdf" android:scheme= "file"/> <data Androi              d:host= "*"      Android:mimetype= "*/*" android:pathpattern= ". *\\.jpg" android:scheme= "file"/&                            Gt <data android:host= "*" android:mimetype= "*/*" Android:pathpatt                    Ern= ". *\\.bmp" android:scheme= "file"/> <data android:host= "*" Android:mimetype= "*/*" android:pathpattern= ". *\\.png" Android:sch Eme= "file"/> <data android:host= "*" Android:mi                            Metype= "*/*" android:pathpattern= ". *\\.txt" android:scheme= "file"/> </intent-filter> </activity>

Then receive this intent on the oncreate of the activity:

Intent Intent = this.getintent (); int flags = Intent.getflags (); if (Flags & INTENT.FLAG_ACTIVITY_LAUNCHED_FROM_ History) = = 0) {if (intent.getaction () = null && Intent.ACTION_VIEW.equals (Intent.getaction ())) {if (scheme_ File.equals (Intent.getscheme ()) | | Scheme_content.equals (Intent.getscheme ())) {String I_type = Getintent (). GetType ();//mimportinguri=file:///storage/ Emulated/0/vlog.xmlintent.addflags (intent.flag_grant_read_uri_permission); Mimportinguri = Intent.getData (); System.out.println ("====mimportinguri=" + Mimportinguri); InputStream is = null;try {is = Getcontentresolver (). Openinputstream (Mimportinguri);} catch (Exception e) {System.out.println ("====e=" +e); if (Mimportinguri! = null && scheme_file.equalsignorecase (Mimportinguri.getscheme ()))) {//is Filestarttocopyfile (IS);} else if (Mimportinguri! = null && scheme_content.equalsignorecase (Mimportinguri.getscheme ())) {Startcopymedia (IS);}}}

If you find that the file you want to import is in media format

Private Boolean Startcopymedia (InputStream is) {Cursor c = null;try {c = getcontentresolver (). Query (Mimportinguri, new STR Ing[] {MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE}, NULL, NULL, NULL); if (c! = null && C . Movetofirst ()) {int dn_index = C.getcolumnindexorthrow (MediaStore.MediaColumns.DISPLAY_NAME); fromfilename = C.getstring (dn_index); int s_index = C.getcolumnindexorthrow (MediaStore.MediaColumns.SIZE); fromfilesize = C.getlong ( S_index);}} catch (Exception e) {System.out.println ("===query e=" +e);} return true;}

If you find that the file to be imported is TXT format, then we create a TMP folder under the SD card and copy the file in:

public static void Makesurefileexist (string path) {String separator = File.separator;int index = path.lastindexof (separat OR);p ath = path.substring (0, index); File F = new file (path); F.mkdirs (); File F1 = new file (path), try {f1.createnewfile ();} catch (IOException e) {e.printstacktrace ()}}       public static String Getsdpath () {File sddir = null; Boolean sdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MED   ia_mounted); Determine if the SD card exists if (sdcardexist) {sddir = Environment.getexternalstoragedirectory ();//Get       Directory} if (Sddir!=null) {return sddir.tostring ();      }else return null; }private string getName (Uri uri) {string str = uri.tostring (); int index = Str.lastindexof (file.separator); String name = str.substring (Index,str.length ()); return name;} Private Boolean Starttocopyfile (InputStream is) {String fileName = Getsdpath () +tmppath+file.separator+getname ( MimportinGUri); makesurefileexist (FileName); File ToFile = new file (fileName); Copythread mcopythread = new Copythread (is, tofile); new Thread (Mcopythread). Start (); return true;} Private class Copythread implements Runnable {private File tofile;private inputstream fosfrom = null;public Copythread (Inp Utstream Fosfrom, File tofile) {this.fosfrom = Fosfrom;this.tofile = ToFile;} @Overridepublic void Run () {try {TimeUnit.MILLISECONDS.sleep (800); FileInputStream Fosfrom = null;if (This.fosfrom! = null) {Fosfrom = (FileInputStream) This.fosfrom;} FileOutputStream Fosto = new FileOutputStream (tofile); byte bt[] = new Byte[1024];int C;int time = 0;while ((c = fosfrom.re AD (BT)) > 0) {fosto.write (BT, 0, C);} if (fosfrom! = null) {Fosfrom.close ();} Fosto.close ();}  catch (Exception e) {return;} finally {try {if (this.fosfrom! = null) {This.fosfrom.close ();}} catch (IOException e) {}}}}

Code: http://download.csdn.net/detail/baidu_nod/7712765

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.