Android Upload of File to Server

后端 未结 3 356
我寻月下人不归
我寻月下人不归 2020-12-20 03:39

Based on the recommendations of a previous post I\'m trying to use Android: Uploading image on server with php however I get a file not found exception.

Here\'s my f

相关标签:
3条回答
  • 2020-12-20 04:12

    i get a file not found exception

    That is because neither of those are paths to files. You can tell that by looking at them. You also did not follow the instructions from my previous answer.

    Replace:

    FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
    

    with:

    InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));
    

    (and replace occurrences of fileInputStream with contentInputStream for the rest of your method)

    Note that:

    • This assumes that your doFileUpload() is implemented on some class that inherits from Context, such as an Activity or a Service. You will need to arrange to get a ContentResolver to doFileUpload() by other means if doFileUpload() does not have access to getContentResolver().

    • You could simplify matters a bit by passing in the Uri you received into doFileUpload(), rather than converting it to a String and then back into a Uri.

    • You will need to invent your own filename for the Content-Disposition: header, as you do not get a filename from the Uri.

    0 讨论(0)
  • 2020-12-20 04:13

    In such scenarios, there can be different reasons. First, make sure that you have added READ_EXTERNAL_STORAGE permission in your AndroidManifest, and also you have added Runtime permissions for API level 19 and above. Then one of the reasons could be mentioned in the accepted answer. But those who are looking for a full detailed solution to upload all types of files to Server in Android can follow this article. Upload File to Server in Android. It describes all steps from picking file to the uploading.

    0 讨论(0)
  • 2020-12-20 04:14

    You are trying to get an InputStream from a Uri ? This might be easier :

    private void doFileUpload(Uri fileUri){
        // some code
        InputStream inputStream = getContentResolver().openInputStream(fileUri);
        // more code
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "some_file_name" +"\"" + lineEnd);
        // the rest of the code
    }
    
    0 讨论(0)
提交回复
热议问题