Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:54+00:00 2026-06-18T02:21:54+00:00

I need to send a file from an Android application (200 – 500 kb).

  • 0

I need to send a file from an Android application (200 – 500 kb). I have tried selected scripts, but these have returned errors.

private void doFileUpload(String filename) {
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String existingFileName = filename;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String responseFromServer = "";
    String urlString = "http://scary-story.ru/";

    try {
        //------------------ CLIENT REQUEST
        FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
        // open a URL connection to the Servlet
        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // read file and write it into form...
        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);
        }

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        Log.e("Debug","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe) {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }


    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());
        String str;

        while ((str = inStream.readLine()) != null) {
            Log.e("Debug","Server Response "+str);
        }
        inStream.close();
    }
    catch (IOException ioex) {
         Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }
}

This script return next error and app force close.

01-28 16:54:20.729: E/AndroidRuntime(5647): FATAL EXCEPTION: main
01-28 16:54:20.729: E/AndroidRuntime(5647): android.os.NetworkOnMainThreadException
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1100)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at java.net.InetAddress.getAllByName(InetAddress.java:251)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.example.testapp1.MainActivity.doFileUpload(MainActivity.java:342)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.example.testapp1.MainActivity.stopRecording(MainActivity.java:186)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.example.testapp1.MainActivity.access$2(MainActivity.java:170)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.example.testapp1.MainActivity$1.onClick(MainActivity.java:302)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.view.View.performClick(View.java:3524)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.view.View$PerformClick.run(View.java:14226)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.os.Handler.handleCallback(Handler.java:605)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.os.Looper.loop(Looper.java:137)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at android.app.ActivityThread.main(ActivityThread.java:4516)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at java.lang.reflect.Method.invoke(Method.java:511)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-28 16:54:20.729: E/AndroidRuntime(5647):     at dalvik.system.NativeStart.main(Native Method)

Excuse my English, I’m Ukrainian student.

PS: Sending images using Http Post – this method not work

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:21:55+00:00Added an answer on June 18, 2026 at 2:21 am

    NetworkOnMainThreadException is occured when you do Network related operations on Main Thread. Refer this link to solve your issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send a file to an Android application from my server. I
I have to Send Byte Array to my java application From Android device.I m
I need to be able to send a file to another website from server-side
I have a file, and I need to send its contents to a function.
HI I have an xml file with 500KB size which i need to send
I have an XML file which I need to parse using PHP and send
I would like to somehow send a file from my app on one android
I need to send an image (as a downloadable file) from an ASP web
after processing a upload file received from an external website, we need to send
I have developed an android .apk file and I need to put it on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.