2014-02-27 59 views
1

我是Android新手。我試圖上傳任何類型的文件(如音頻,視頻,文檔,圖像..格式)從Android SD卡到PHP服務器,但沒有得到明確code.I得到了一些解決方案here,但他們提到了現有的特定類型文件&具體路徑。我想從SD卡上所有類型的fie上傳。使用Android從SD卡上的任何文件夾上傳任何文件

+0

在鏈路上的延伸既不MIME類型不過濾的代碼,這應該爲你工作。 –

+0

感謝您的快速反應Loic。我不知道如何上傳我的類型和從SD卡中選擇文件。我不想提及現有的文件名。如果有想法與我分享。 – Raj

回答

1

此方法ü可以訪問SD卡中的所有文件 這是我的代碼

public void getAllFile(File dir) { 
       File listFile[] = dir.listFiles(); 

       if (listFile != null) { 
        for (int i = 0; i < listFile.length; i++) { 

         if (listFile[i].isDirectory()) { 
          getAllFile(listFile[i]); 
         } else { 
          listFile[i].getName(); 

           System.out.println("your file is "+listFile[i].getName()); 
          } 
         } 

       } 
     } 

好運夥計:)的

0

//調用此方法從設備內存中選擇文件

static Uri url; 
    String path; 

private void showFileChooser() { 
    Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
    i.setType("*/*"); 
    i.addCategory(Intent.CATEGORY_OPENABLE); 
    i = Intent.createChooser(i, "Choose a file"); 

    startActivityForResult(i, FILE_SELECT_CODE); 
} 

//在這裏你會得到所選文件的路徑

@Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      if(resultCode==-1) 
      { 
       Log.w("Request Code", ""+requestCode); 
       Log.w("Result Code", ""+resultCode); 
       path = getRealPathFromURI(data.getData()); 
       try { 
        Log.w("Intent", data.getData().toString()); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        Log.w("Error", e.toString()); 
       } 
       } 
     } 

//獲取文件的準確路徑 - 確切的路徑存儲在路徑變量

public String getRealPathFromURI (Uri contentUri) 
{ 
    String path = null; 
    String[] proj = { MediaStore.MediaColumns.DATA }; 

     if("content".equalsIgnoreCase(contentUri.getScheme())) 
      { 
       Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null); 
       if (cursor.moveToFirst()) { 
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
        path = cursor.getString(column_index); 
       } 
       cursor.close(); 
       return path; 
      } 
      else if("file".equalsIgnoreCase(contentUri.getScheme())) 
      { 
       return contentUri.getPath(); 
      } 
      return null; 
     } 
相關問題