【问题标题】:upload any file from any folder from sd card to server using Android使用 Android 将任何文件夹中的任何文件从 sd 卡上传到服务器
【发布时间】:2014-03-30 10:56:17
【问题描述】:

我是 Android 新手。我正在尝试将任何类型的文件(如音频、视频、文档、图像..格式)从 android SD 卡上传到 php 服务器,但没有得到清晰的代码。我得到了一些解决方案here,但他们提到了现有的特定文件类型和特定路径。我想从 SD 卡上传所有类型的文件。

【问题讨论】:

  • 链接中的代码不会过滤扩展程序,也不会过滤 mime 类型,这应该适合您。
  • 感谢 Loic 的快速回复。我不知道如何上传我的类型并从 SD 卡中选择文件。我不想提及现有文件名。如果有想法分享我。

标签: java php android


【解决方案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());
                            }
                        }

                }
         }

祝你好运:)

【讨论】:

    【解决方案2】:

    //调用该方法从设备内存中选择文件

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

    //获取文件的准确路径 -- 准确路径保存在path变量中

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

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 1970-01-01
      • 2014-08-14
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2013-04-13
      相关资源
      最近更新 更多