源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android 文件选择的实现代码

  • 时间:2020-06-15 18:30 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android 文件选择的实现代码
打开文件选择器
[u]复制代码[/u] 代码如下:
private void showFileChooser() {     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);     intent.setType("*/*");     intent.addCategory(Intent.CATEGORY_OPENABLE);     try {         startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);     } catch (android.content.ActivityNotFoundException ex) {         Toast.makeText(this, "Please install a File Manager.",  Toast.LENGTH_SHORT).show();     } }
选择的结果
[u]复制代码[/u] 代码如下:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)  {     switch (requestCode) {         case FILE_SELECT_CODE:              if (resultCode == RESULT_OK) {              // Get the Uri of the selected file             Uri uri = data.getData();             String path = FileUtils.getPath(this, uri);         }                   break;     } super.onActivityResult(requestCode, resultCode, data); }
FileUtils文件
[u]复制代码[/u] 代码如下:
public class FileUtils {     public static String getPath(Context context, Uri uri) {         if ("content".equalsIgnoreCase(uri.getScheme())) {             String[] projection = { "_data" };             Cursor cursor = null;             try {                 cursor = context.getContentResolver().query(uri, projection,null, null, null);                 int column_index = cursor.getColumnIndexOrThrow("_data");                 if (cursor.moveToFirst()) {                     return cursor.getString(column_index);                 }             } catch (Exception e) {                 // Eat it             }         }         else if ("file".equalsIgnoreCase(uri.getScheme())) {             return uri.getPath();         }         return null;     } }
这个很简单。 出处:http://www.cnblogs.com/linlf03/
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部