2017-08-28 4 views
0

Ich brauche die Lösung für SDK-Version 17 oder darunterWie bekomme ich den xls-Dateipfad von URI für SDK 17 oder niedriger für Android?

das ist meine Methode.

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

i verwenden diese Methode aber Cursor-Wert null zurück

Hilfe

+0

schauen meine Antwort hier https://stackoverflow.com/questions/45913001/pick-any-file-using-intent-in-android/45913122#45913122 –

+0

getan, was ich brauche nur myuri.getPath(); keine Notwendigkeit diese Methode –

+0

Sie erhalten Pfad an dieser Zeile "Uri Daten = result.getData();" –

Antwort

1

path = uri.getPath(); Nur diese Zeile wird verwendet, um den Pfad der ausgewählten Datei abzurufen.

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

funktioniert wirklich diesen Code –

Verwandte Themen