zoukankan      html  css  js  c++  java
  • Android 文件的选择

    Android 文件的选择

    打开文件选择器

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

      

    选择的结果

        @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文件

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

      这个很简单。

     

  • 相关阅读:
    【微积分】 02
    【微积分】 01
    【线性代数】 09
    云南国庆八日游策划书
    Kubectl工具常用命令
    Linux 常用命令缩写及对应的
    kubectl工具的windows安装方法
    Intellij IDEA工具的常用快捷键
    如何理解docker镜像build中的上下文
    【转】在服务器上排除问题的头五分钟&常用命令
  • 原文地址:https://www.cnblogs.com/linlf03/p/3267732.html
Copyright © 2011-2022 走看看