How to get selected xls file path from uri for SDK 17 or below for android?

前端 未结 2 1383
忘了有多久
忘了有多久 2020-11-29 13:23

I need the solution for sdk version 17 or below

this is my method.

    public static String getRealPathFromURI_API11to18(Context context, Uri content         


        
2条回答
  •  春和景丽
    2020-11-29 13:43

    path = uri.getPath(); only this line is used for get the path of selected file.

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
                intent.setType("application/vnd.ms-excel"); 
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                try {
                    startActivityForResult(
                            Intent.createChooser(intent, "Select a File to Upload"),
                            FILE_SELECT_CODE);
                } catch (android.content.ActivityNotFoundException ex) {
                    // Potentially direct the user to the Market with a Dialog
                    Toast.makeText(getApplicationContext(), "Please install a File Manager.", 
                            Toast.LENGTH_SHORT).show();
                }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub   
        switch (requestCode) {
        case FILE_SELECT_CODE:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file 
            Uri uri = data.getData();       
            // Get the path
            try {   
                    path = uri.getPath();                                       
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        break;
    }
        super.onActivityResult(requestCode, resultCode, data);
    }
    

提交回复
热议问题