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

  • Home
  • SEARCH
  • 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 9245087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:11:12+00:00 2026-06-18T09:11:12+00:00

I am creating a simple app on Android SDK. I created an Android Client

  • 0

I am creating a simple app on Android SDK. I created an Android Client and Java Server. Now what I have created is that Android app browse for image from gallery and show the Path on the screen. Also, it displays the image, which was showed. To start the sending connection thing firstly I run the Java Server. Then I run the app on my phone. I choose the photo and then press send. The app fezzes after the push and forces to close. Below you can see my code. Any ideas?

Android Client

package com.example.workingclient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SendfileActivity extends Activity {
    /** Called when the activity is first created. */

    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("34");
        img = (ImageView) findViewById(R.id.ivPic);
        System.out.println("36");
        ((Button) findViewById(R.id.bBrowse))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        System.out.println("40");
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                Intent.createChooser(intent, "Select Picture"),
                                SELECT_PICTURE);
                        System.out.println("47");
                    }
                });
        ;
        System.out.println("51");
        Button send = (Button) findViewById(R.id.bSend);
        final TextView status = (TextView) findViewById(R.id.tvStatus);

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Socket sock;
                try {
                    sock = new Socket("192.168.0.3", 27015); 
                    System.out.println("Connecting...");

                     // sendfile
                          File myFile = new File (selectedImagePath); 
                          byte [] mybytearray  = new byte [(int)myFile.length()];
                          FileInputStream fis = new FileInputStream(myFile);
                          BufferedInputStream bis = new BufferedInputStream(fis);
                          bis.read(mybytearray,0,mybytearray.length);
                          OutputStream os = sock.getOutputStream();
                          System.out.println("Sending...");
                          os.write(mybytearray,0,mybytearray.length);
                          os.flush();

                        sock.close();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                TextView path = (TextView) findViewById(R.id.tvPath);
                path.setText("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}

Android main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />

    <TextView
        android:id="@+id/tvPath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Path: " />

    <Button
        android:id="@+id/bBrowse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Browse" >
    </Button>

    <Button
        android:id="@+id/bSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />

    <ImageView
        android:id="@+id/ivPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageView>

</LinearLayout>

Android AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.workingclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.workingclient.SendfileActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

Java Server

import java.io.*;
import java.net.*;

public class FileServer {

    public static void main(String[] args) throws IOException {
        int filesize = 6022386; // filesize temporary hardcoded

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;

        // create socket
        ServerSocket servsock = new ServerSocket(27015);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // receive file
            byte[] mybytearray = new byte[filesize];
            InputStream is = sock.getInputStream();
            FileOutputStream fos = new FileOutputStream(
                    "C:\\Users\\5750G\\Desktop\\Test.jpg"); // destination
                                                                    // path and
                                                                    // name of
                                                                    // file
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray, 0, mybytearray.length);
            current = bytesRead;

            // thanks to A. Cádiz for the bug fix
            do {
                bytesRead = is.read(mybytearray, current,
                        (mybytearray.length - current));
                if (bytesRead >= 0)
                    current += bytesRead;
            } while (bytesRead > -1);

            bos.write(mybytearray, 0, current);
            bos.flush();
            long end = System.currentTimeMillis();
            System.out.println(end - start);
            bos.close();

            sock.close();
        }
    }

}

Error

02-05 17:26:21.871: I/Start Server Button Clicked(22924): yipee
02-05 17:26:21.871: D/AndroidRuntime(22924): Shutting down VM
02-05 17:26:21.871: W/dalvikvm(22924): threadid=1: thread exiting with uncaught exception (group=0x40c5e1f8)
02-05 17:26:21.876: E/AndroidRuntime(22924): FATAL EXCEPTION: main
02-05 17:26:21.876: E/AndroidRuntime(22924): android.os.NetworkOnMainThreadException
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at libcore.io.IoBridge.connect(IoBridge.java:112)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.Socket.startupSocket(Socket.java:566)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.Socket.tryAllAddresses(Socket.java:127)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.Socket.<init>(Socket.java:177)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.net.Socket.<init>(Socket.java:149)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at com.example.workingclient.SendfileActivity$2.onClick(SendfileActivity.java:93)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.view.View.performClick(View.java:3627)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.view.View$PerformClick.run(View.java:14329)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.os.Handler.handleCallback(Handler.java:605)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.os.Looper.loop(Looper.java:137)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at android.app.ActivityThread.main(ActivityThread.java:4511)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at java.lang.reflect.Method.invoke(Method.java:511)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-05 17:26:21.876: E/AndroidRuntime(22924):    at dalvik.system.NativeStart.main(Native Method)
02-05 17:26:47.916: I/Process(22924): Sending signal. PID: 22924 SIG: 9
02-05 17:36:15.351: I/System.out(23357): 34
02-05 17:36:15.351: I/System.out(23357): 36
02-05 17:36:15.351: I/System.out(23357): 51
02-05 17:36:15.416: D/CLIPBOARD(23357): Hide Clipboard dialog at Starting input: finished by someone else... !
  • 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-18T09:11:13+00:00Added an answer on June 18, 2026 at 9:11 am

    You have a NetworkOnMainThreadException. The solution is to put the upload code in its own thread or in an AsyncTask.

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

Sidebar

Related Questions

I am creating a simple Click Counter Android App using Java. I am new
I am creating a simple Click Counter Android App using Java. I am new
I'm creating an app on android in java, very simple, which has a button
I'm new to android programming and I was creating a simple app from a
I'm currently creating my first android app and I've a simple class 'Player' I
I am creating simple app on Silverlight 4. In folder of View I have
I am creating an app that will point a simple arrow in the direction
I'm creating a simple app that use google maps in a tab and something
I am creating an Android app that will download and cache PDF files (the
I am creating just a simple android app which will respond to received. I

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.