1

I'm writing a code that allows my users to upload any kind of file , this is my code:

Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
            fileIntent.setType("*/*"); // intent type to filter application based on your requirement
            startActivityForResult(fileIntent, 1);

users are able to select their files and then I get the path and file name via this code :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            Uri u = data.getData();
                filepath = u.getPath();
                filename = u.toString();
                chooseFile=true;



        }
    }
}

this is the my code for sending the selected file to server , I'm using assyctask . this is the code ;

@Override
    protected String doInBackground(String... params) {
        if (filepath != null) {//file entekhab shode bud
            int serverResponseCode = 0;
            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            DataInputStream inputStream = null;
            String urlServer = getString(R.string.url)+"/getFile.php?id="+sefareshId;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";

            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;

            try {
                FileInputStream fileInputStream = new FileInputStream(new File(filepath));

                URL url = new URL(urlServer);
                connection = (HttpURLConnection) url.openConnection();

                // Allow Inputs & Outputs.
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);

                // Set HTTP method to POST.
                connection.setRequestMethod("POST");

                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

                outputStream = new DataOutputStream(connection.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filepath + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);

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

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

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

                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                serverResponseCode = connection.getResponseCode();

                fileInputStream.close();
                outputStream.flush();
                outputStream.close();
            } catch (Exception ex) {
                Log.v("this", ex.getMessage());
            }
            return serverResponseCode + "";
        } else
            return "5";

    }

by running this code ,I get this error:

/document/image:134342: open failed: ENOENT (No such file or directory)

I think the url path of this file is wrong , How can I get the correct path of file and then upload it ?

thanks

navid abutorab
  • 189
  • 1
  • 2
  • 9
  • you do not have to post an asynctask and upload code in order to show if a file exists or not. You should check if a file exists in onActivityResult directly. So please adapt your code and the post so we can concentrate on the issue. – greenapps Oct 05 '16 at 08:46
  • When done, start telling us the values of u.getPath(); and u.toString(); – greenapps Oct 05 '16 at 08:48
  • do some debugging, put a break point on this sentence `String result = data.getStringExtra("result");` and tell us what u see, and also on what Android version you are doing this? – Mohammad Haidar Oct 05 '16 at 08:48
  • 1
    /document/image:134342 is what you get with u.getPath(); You should use u.toString() instead. What you get then is a content sheme path. You cannot use FileInputStream for that. Instead use an InputStream with getContentResolver().openInputStream() on the uri. Dont panic. You only have to add a few lines of code. – greenapps Oct 05 '16 at 08:54
  • you need to check the log of u.getPath(). are you getting value in that and then convert in real path. example : http://stackoverflow.com/questions/8646246/uri-from-intent-action-get-content-into-file – kalpana c Oct 05 '16 at 09:11

0 Answers0