【问题标题】:Uploading pdf to server from android从android上传pdf到服务器
【发布时间】:2016-03-20 09:41:09
【问题描述】:

我正在尝试将 pdf 文件从 android mobile 上传到服务器。我尝试使用图像,它适用于图像。但是当尝试上传 pdf 文件时,它给了我错误。

我正在使用 Eclipse API 18

我试过这个: 在列表项上单击它会打开一个文件资源管理器

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    int sub=subid.get(position);
    Toast.makeText(getApplicationContext(),Integer.toString(sub),Toast.LENGTH_LONG).show();
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");
    startActivityForResult(intent, 1);

}
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {


        // TODO Auto-generated method stub
        //super.onActivityResult(requestCode, resultCode, data);
        String filepath;
        if (resultCode==RESULT_OK) {
            filepath=getRealPathFromURI_API11to18(UploadAssignment.this, data.getData());
            System.out.println("File "+filepath);

        }


        //new Upload(UploadAssignment.this, namepath,Integer.toString(prn),sname,Integer.toString(sub)).execute();
    //    } 
    }


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

这里的 getRealPathFromURI_API11to18() 方法返回 null。 我不知道为什么它给了我空值。

【问题讨论】:

  • 此内容可确保您仅浏览图像文件。用户 MediaStore.Images.Media.DATA MediaStore.Files 浏览 pdfs
  • @Honza Musil 我们应该使用什么来上传 pdf。文件。

标签: android pdf upload


【解决方案1】:

您将设置类型“application/pdf”传递给意图,并且在获取 URI 后,您尝试使用 MediaStore.Images.Media.DATA 获取 RealPath,该方法仅用于获取图像的路径.它不允许从 URI 中读取 pdf 文件的路径。您可以尝试以下方法:

 try{

         /*Used if you want to upload file object*/
        File mFile = new File(new URI(mNewPath));

        /*Used if you want to read absolute path of that specific file*/
        String mPath = mFile.getAbsolutePath();

    }catch (Exception e){

    }

【讨论】:

  • 感谢您的回复,我已经厌倦了它给我的错误:java.net.URISyntaxException: Illegal character in path at index 42: /document/3565-3161:OnlineStatementOfMarks (1).pdf我还打印了在 logcat 上显示的 uri 路径:content://com.android.externalstorage.documents/document/3565-3161%3AOnlineStatementOfMarks%20(1).pdf
  • 03-20 16:27:58.754: I/System.out(24744): Excp java.lang.IllegalArgumentException: URI 不是绝对的: %2Fdocument%2F3565-3161%3AOnlineStatementOfMarks+%281%29 .pdf
  • 我无法获得 .replaceAll("%20"," ");在我的 uri 上
  • 我试过这个 String may=path.getPath().toString().replaceAll("%20"," ");我收到错误 03-20 16:38:42.784: I/System.out(26818): Excp java.net.URISyntaxException: Illegal character in path at index 42: /document/3565-3161:OnlineStatementOfMarks (1).pdf
  • 您好,您的文件名和路径似乎包含一些特殊字符和空格。所以尝试在那个字符串上应用 trim()
【解决方案2】:

你可以通过这个链接upload file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-16
    • 2012-04-06
    • 2015-07-13
    • 2011-06-28
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多