1

I have been trying to upload an audio file from android to an ec2 server but can't get the file name of the selected file.

Uri uri = data.getData();
String selectedPath = uri.toString();

I tried the above code and got the output as:

content://com.android.providers.downloads.documents/document/17

but when I tried to upload the file it doesn't recognize the path as a file.

Here is my Upload.java code:

public class Upload {

Context context;
public Upload(Context context) {
    this.context = context;
}

String UPLOAD_URL= "<MY_SERVER_URL>";

private int serverResponseCode;

public String uploadVideo(String file) {

    String fileName = file;
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;

    File sourceFile = new File(file);
    if (!sourceFile.isFile()) {
        Log.e("Huzza", "Source File Does not exist");
        return null;
    }

    try {
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        URL url = new URL(UPLOAD_URL);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        conn.setRequestProperty("myFile", fileName);
        dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        Log.i("Huzza", "Initial .available : " + bytesAvailable);

        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        serverResponseCode = conn.getResponseCode();

        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (serverResponseCode == 200) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                    .getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
        } catch (IOException ioex) {
        }
        return sb.toString();
    }else {
        return "Could not upload";
    }
}
}

And this is the caller method

private void uploadVideo() {
    class UploadVideo extends AsyncTask<Void, Void, String> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.e("STRING", s);

        }

        @Override
        protected String doInBackground(Void... params) {
            Upload u = new Upload(getApplicationContext());
            String msg = u.uploadVideo(selectedPath);
            //Log.e("MSG", msg);
            return msg;
        }
    }
    UploadVideo uv = new UploadVideo();
    uv.execute();
}

I've tried all the solutions present but it turns out that most of those solutions were for Android KitKat and below and other ones didn't work too. None of the solutions works for Android Pie.

Tried Solutions:

How to get the Full file path from URI

Get real path from URI, Android KitKat new storage access framework

Get filename and path from URI from mediastore

parth.singh71
  • 354
  • 1
  • 3
  • 10
  • _"Secondly, I really don't have any idea how to handle the incoming audio file with python. So any help on that will be appreciated."_ That should be a separate question. – Markus Kauppinen Oct 07 '19 at 12:15
  • Thankyou for your response, I removed the extra part and will post it as a different question. – parth.singh71 Oct 07 '19 at 16:54

0 Answers0