android中activity可以響應外部的action的例子(可以用這個來匯入外部檔案)

來源:互聯網
上載者:User

android中activity可以響應外部的action的例子(可以用這個來匯入外部檔案)

如果我們在檔案瀏覽器中點擊一個檔案,然後點擊它,會彈出一個列表給你選擇

 

 

如果使自己的應用也出現在這個列表上,必須在menifest的這個activity下加入:

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

然後在activity的oncreate上接收這個intent:

 

 

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);}}}}

這裡如果發現要匯入的檔案是media的格式

 

 

private boolean startCopyMedia(InputStream is) {Cursor c = null;try {c = getContentResolver().query(mImportingUri, new String[] {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;}

如果發現要匯入的檔案是txt格式,則我們在sd卡下建立一個tmp的檔案夾,把這個檔案拷貝進去:

 

 

public static void makesureFileExist(String path) {String separator = File.separator;int index = path.lastIndexOf(separator);path = 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.MEDIA_MOUNTED);   //判斷sd卡是否存在       if(sdCardExist)         {                        sdDir = Environment.getExternalStorageDirectory();//擷取跟目錄      }         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(InputStream 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.read(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) {}}}}
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.