0

你好,我是Android新手,我正在做一個應用程序,我可以從SD卡中選擇文件並獲取該文件的路徑。我遵循this教程。並能夠從內部存儲器獲得文件路徑,但不幸的是我無法從SD卡獲取路徑。如何在選擇文檔時從外部SD卡獲取路徑?

活動

public class Buttona extends Activity { 
    private static final int MY_INTENT_CLICK=302; 
    String selectedFilePath; 
    TextView texta; 
    Context mContext; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.buttona); 
    } 

    // Start the service 
    public void startService(View view) { 

      if (Build.VERSION.SDK_INT < 19) { 
       Intent intent = new Intent(); 
       intent.setType("*/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, "Select File"), MY_INTENT_CLICK); 
      } else { 
       Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 
       intent.addCategory(Intent.CATEGORY_OPENABLE); 
       intent.setType("*/*"); 
       startActivityForResult(intent, MY_INTENT_CLICK); 
      } 

    } 


    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (resultCode == RESULT_OK) 
     { 
      if (requestCode == MY_INTENT_CLICK) 
      { 
       try { 
        if (data != null) { 

         Uri uri = data.getData(); 

         String filePath = getPath(mContext, uri); 
         String fileName = getFileName(data, mContext); 



         Log.e("TAG", filePath); 
         Log.e("TAG", fileName); 




        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    public static String getFileName(Intent data,Context context){ 

     Uri uri = data.getData(); 
     String uriString = uri.toString(); 
     File myFile = new File(uriString); 
     String path = myFile.getAbsolutePath(); 
     String displayName = null; 

     if (uriString.startsWith("content://")) { 
      Cursor cursor = null; 
      try { 
       cursor =context. getContentResolver().query(uri, null, null, null, null); 
       if (cursor != null && cursor.moveToFirst()) { 
        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 
       } 
      } finally { 
       cursor.close(); 
      } 
     } else if (uriString.startsWith("file://")) { 
      displayName = myFile.getName(); 
     } 


     return displayName; 
    } 

//get file path 

    public static String getPath(Context context, Uri uri) throws URISyntaxException { 
     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; 
    } 

    // Stop the service 
    public void stopService(View view) { 
     stopService(new Intent(this, MyService.class)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

} 
+0

並不是每一個文件'Uri'必須有一個文件路徑,因爲他們沒有被文件。並非每個文件都必須有一個對您有用的文件路徑(例如,該文件可以被加密)。而且,在可移動存儲上,即使文檔是文件,也無法通過文件系統訪問它。 [使用'Uri'作爲標識符,然後使用'ContentResolver'和類似'openInputStream()'的方法來處理內容](https://commonsware.com/blog/2016/03/15/how-consume-內容uri.html)。 – CommonsWare

+0

請舉例 –

+0

https://github.com/commonsguy/cw-omnibus/tree/master/Documents/Cumerumer https://github.com/commonsguy/cw-omnibus/tree/master/Documents/TinyTextEditor – CommonsWare

回答

-1
  try { 
       if (data != null) { 

        Uri uri = data.getData(); 

        String filePath = getPath(mContext, uri); 
        String fileName = getFileName(data, mContext); 



        Log.e("TAG", filePath); 
        Log.e("TAG", fileName); 




       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

獲得文件的路徑和文件名

//get file name 
public static String getFileName(Intent data,Context context){ 

    Uri uri = data.getData(); 
    String uriString = uri.toString(); 
    File myFile = new File(uriString); 
    String path = myFile.getAbsolutePath(); 
    String displayName = null; 

    if (uriString.startsWith("content://")) { 
     Cursor cursor = null; 
     try { 
      cursor =context. getContentResolver().query(uri, null, null, null, null); 
      if (cursor != null && cursor.moveToFirst()) { 
       displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 
      } 
     } finally { 
      cursor.close(); 
     } 
    } else if (uriString.startsWith("file://")) { 
     displayName = myFile.getName(); 
    } 


    return displayName; 
} 

//get file path 

public static String getPath(Context context, Uri uri) throws URISyntaxException { 
    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; 
} 
+0

什麼是myFile.getAbsolutePath()&& myFile.getName()? –

+0

@AnshulKhare getPath和getFileName是兩個靜態方法,如果你想獲得文件路徑,而不僅僅是使用getPath()方法,你的onActivityResult只是傳遞參數,在響應你將得到你選擇的filePath –

+0

我得到的錯誤無法解析getAbsolutePath();並且無法解析getName(); –

相關問題