</pre>1、打开系统自带的文件管理器</h2><div><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><pre name="code" class="java">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivity(intent);
</pre>1、打开系统自带的文件管理器</h2><div><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><pre name="code" class="java">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivity(intent);
2、打开特定文件
File file = new File(Environment.getExternalStorageDirectory(),"/Download/view的原理.txt"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), getMimeType(file)); startActivity(intent);
protected String getMimeType(File file) { String type = "*/*"; if(file != null){ String name = file.getName(); int lastIndexOf = name.lastIndexOf("."); String extension = name.subSequence(lastIndexOf+1, name.length()).toString().toLowerCase(); System.out.println("wisely extendsion:"+extension); if(MimeTypeMap.getSingleton().hasExtension(extension)){ type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); } } return type; }
经测试,会弹出上面的窗体,选择打开该文件的应用。txt文件测试过,正常,其它文件类型未测试。
这里面用到了一个MimeTypeMap的类,它是系统用来管理MimeType的管理类。 里面的静态方法getSingleton()中可以查看到该版本SDK支持的所有MIME-TYPE。它的内部维护了2个map集合,其中一个以mimeType为键,extension为值;另一个以extension为键,mimeType为值。
该类有4个关键方法:
- 1、boolean hasMimeType(String mimeType):返回该版本sdk中是否包含该mimeType
- 2、String getMimeTypeFromExtension(String extension):根据extension获取mimeType
- 3、boolean hasExtension(String extension):该extension是否存在于该sdk版本中。
- 4、String getExtensionFromMimeType(String mimeType):通过mimeType获取extension。
除了上述4个方法外,还有2个方法,一个是获取单例对象的static MimeTypeMap getSingleton(),另一个方法是从url中获取extension的static String getFileExtensionFromUrl(String url)。
3、设置activity为透明模式
<style name="TransparentActivity" parent="AppBaseTheme"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> </style>
用途:用于模拟Dialog效果,比如在Service中没法用Dialog,就可以用Activity来模拟
4、获取Bitmap对象的大小
//android.os.Build //Build.VERSION_CODES.HONEYCOMB_MR1 = 12 public long getBitmapSize(Bitmap bitmap){ <span style="white-space:pre"> </span>if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1){ return bitmap.getByteCount(); } else { return bitmap.getRowBytes()*bitmap.getHeight(); } }
5、各种路径的获取
<span style="white-space:pre"> </span>1、this.getCacheDir().getAbsoluteFile(); /data/data/包名/cache 2、this.getDir("module", Context.MODE_PRIVATE).getAbsoluteFile(); /data/data/包名/app_module 3、this.getFileStreamPath("download").getAbsoluteFile(); /data/data/包名/files/download 4、this.getFilesDir().getAbsoluteFile(); /data/data/包名/files 5、Environment.getDataDirectory().getAbsoluteFile(); /data 6、Environment.getDownloadCacheDirectory(); /cache 7、Environment.getRootDirectory().getAbsoluteFile(); /system 8、this.getDatabasePath("aaa.db").getAbsoluteFile(); /data/data/包名/databases/aaa.db 9、this.getExternalCacheDir().getAbsoluteFile(); /storage/sdcard0/Android/data/包名/cache 10、this.getObbDir().getAbsoluteFile(); /storage/sdcard0/Android/obb/包名 11、this.getPackageCodePath(); /data/app/包名-1.apk 12、this.getPackageResourcePath(); /data/app/包名-1.apk
</pre><pre name="code" class="java">补充:
FileOutputStream fos = this.openFileOutput("hello_wisely", Context.MODE_PRIVATE); fos.write("hello,world!!".getBytes()); fos.flush(); fos.close(); 在/data/data/包名/files文件夹下,生成了一个hello_wisely文件(没有后缀),内容为hello,world!!
boolean deleteFile = this.deleteFile("hello_wisely"); 删除/data/data/包名/files文件夹下的hello_wisely文件。
文件读取:
FileInputStream fis = this.openFileInput("hello_wisely"); byte[] buf = new byte[1024]; int lengh = 0; StringBuffer buffer = new StringBuffer(); while((lengh = fis.read()) >0){ buffer.append((char)lengh); } Toast.makeText(this, buffer.toString(), 0).show();
读取到的是 <span style="font-family: Arial, Helvetica, sans-serif;">hello,world!!</span>
好用的属性
1、TextView中的属性:
- android:maxLength="11" 这个属性经常用在TextView的子类EditText中,设置数值之后,可以限制输入的字符个数,如写11,可以限制它为11个字符,如果再限制了它只能输入数字,那么就可以用来校验手机号
-android:digits="0123456789" 这个属性可以和maxLenght属性配合使用,设置在EditText中之后,那么它只会接收里面写的字符,如果没有写进里面,是无法输入的
- android:cursorVisible 设置该属性,可以确定是否显示EditText的光标
尾巴:参考资料
- 1、http://www.cnblogs.com/over140/p/4062209.html