android中activity可以响应外部的action的例子(可以用这个来导入外部文件)
如果我们在文件浏览器中点击一个文件,然后点击它,会弹出一个列表给你选择
如果使自己的应用也出现在这个列表上,必须在menifest的这个activity下加入:
<activity android:name="com.example.clickabletextview.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="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="image/jpeg" /> <data android:mimeType="image/x-ms-bmp" /> <data android:mimeType="image/png" /> <data android:mimeType="image/x-pcx" /> <data android:mimeType="image/targa" /> <data android:mimeType="image/x-photoshop" /> <data android:mimeType="text/plain" /> <data android:mimeType="text/xml" /> <data android:mimeType="text/html" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.pdf" android:scheme="file" /> <data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.jpg" android:scheme="file" /> <data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.bmp" android:scheme="file" /> <data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.png" android:scheme="file" /> <data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.txt" android:scheme="file" /> </intent-filter> </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.xml intent.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 file startToCopyFile(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; } @Override public 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) { } } } }
代码:http://download.csdn.net/detail/baidu_nod/7712765
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。