2017-08-28 52 views
0

我需要的解决方案的SDK版本17或以下如何从uri获取SDK的选定xls文件路径为SDK 17或以下的android?

这是我的方法。

public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    String result = null; 
    CursorLoader cursorLoader = new CursorLoader(
      context, 
      contentUri, proj, null, null, null); 
    Cursor cursor = cursorLoader.loadInBackground(); 

    if(cursor != null){ 
     int column_index = 
       cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     result = cursor.getString(column_index); 
    } 
    return result; 
} 

我用这个方法,但光标值返回null

帮助

+0

看我的答案在这里https://stackoverflow.com/questions/45913001/pick-any-file-using-intent-in-android/45913122#45913122 –

+0

完成我只需要myuri.getPath();不需要这个方法 –

+0

你得到这条线的路径“Uri data = result.getData();” –

回答

1

路径= uri.getPath();只有这一行用于获取选定文件的路径。

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); 
} 
+0

真正在使用此代码 –

相关问题