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 5944451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:32:20+00:00 2026-05-22T16:32:20+00:00

In my app I am trying to capture an image from my device and

  • 0

In my app I am trying to capture an image from my device and I want to upload it to a server.
I am following the answer which was posted here

Following is my code for starting camera

 startBtn = (Button) findViewById(R.id.startBtn);
 startBtn.setOnClickListener(new View.OnClickListener() 
  {
    public void onClick(View v) 
       {
            startCamera();
        }
    });
}

public void startCamera() 
{        
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
    intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
    startActivityForResult( intent, 0 );
}

But after i capture and when i try to upload it gets crashed. In logcat it shows the error
at OnActivity result and in the doFileUpload

Following is my code

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        switch( resultCode )
        {
            case 0:
                break;

            case -1:
                onPhotoTaken();
                break;
        }
    }

    protected void onPhotoTaken()
    {
        doFileUpload(MediaStore.EXTRA_OUTPUT);
    }

    private void doFileUpload(String extraOutput)
    {
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inStream = null;        
        String urlServer = "http:XXXXXXXXXXXXXXXXXXXXXX/upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        FileInputStream fileInputStream = new FileInputStream(new File(extraOutput) );
        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        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=\"userfile\";filename=\"" + extraOutput +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        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);
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();       
        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
     }

Please help me in fixing my error……plz

  • 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-05-22T16:32:21+00:00Added an answer on May 22, 2026 at 4:32 pm

    I am using this code for uploading image from my device to server

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.v("Test", "IGA");
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedImageUri =(Uri)data.getData();
                AddCourtActivity.mImage.setImageURI(selectedImageUri);
                selectedImagePath = getPath(selectedImageUri);
                imagePath=selectedImagePath;
                Log.v("Test","IGA "+imagePath);
            } else if (requestCode == CAMERA_PIC_REQUEST) {
    
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                imagePath=selectedImagePath;
    
                Log.v("Test","IGA1 "+imagePath);
                AddCourtActivity.mImage.setImageBitmap(thumbnail);
            }
        }
        if (resultCode == RESULT_CANCELED) {
    
            return;
        }
    }
    
    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);
    }
    public String getPath1(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow("userImage"/*MediaStore.Images.Media.DATA*/);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    }
    
    //Use this class for upload image to server
    
    
    public class FileUploader {
    
    private String Tag = "UPLOADER";
    private String urlString;// = "YOUR_ONLINE_PHP";
    HttpURLConnection conn;
    String exsistingFileName;
    public String result;
    
    public String uploadImageData(String serverImageTag) {// Server image tag
                                                            // may be
                                                            // courtImage[] or
                                                            // userImage
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {
            // ------------------ CLIENT REQUEST
    
            Log.e(Tag, "Inside second Method");
    
            FileInputStream fileInputStream = new FileInputStream(new File(
                    exsistingFileName));
    
            // 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);
    
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
    
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            if (serverImageTag.equalsIgnoreCase("courtImage")) {
                dos.writeBytes("Content-Disposition: post-data; name=courtImage[];filename="
                        + exsistingFileName + "" + lineEnd);
            } else if (serverImageTag.equalsIgnoreCase("userImage")) {
                dos.writeBytes("Content-Disposition: post-data; name=userImage[];filename="
                        + exsistingFileName + "" + lineEnd);
            }
            dos.writeBytes(lineEnd);
    
            Log.e(Tag, "Headers are written");
    
            // create a buffer of maximum size
    
            int bytesAvailable = fileInputStream.available();
            int maxBufferSize = 1000;
            // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bytesAvailable];
    
            // read file and write it into form...
    
            int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
    
            while (bytesRead > 0) {
                dos.write(buffer, 0, bytesAvailable);
                bytesAvailable = fileInputStream.available();
                bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
            }
    
            // send multipart form data necessary after file data...
    
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
            String serverResponseMessage = conn.getResponseMessage();
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
    
            // String serverResponseCode = conn.
            // String serverResponseMessage = conn.getResponseMessage();
    
            while ((result = rd.readLine()) != null) {
    
                Log.v("result", "result " + result);
    
                    Login.fbResponse = result;
    
            }
            // close streams
            Log.e(Tag, "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
            rd.close();
        } catch (MalformedURLException ex) {
            Log.e(Tag, "error: " + ex.getMessage(), ex);
        }
    
        catch (IOException ioe) {
            Log.e(Tag, "error: " + ioe.getMessage(), ioe);
        }
    
        // Parsing has finished.
        return result;
    }
    
    public FileUploader(String existingFileName, String urlString) {
    
        this.exsistingFileName = existingFileName;
        this.urlString = urlString;
    
    }
    

    Just pass the existingFileName, urlString in the constructor then the file is upload .

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

Sidebar

Related Questions

in my app i am trying to capture an image and i want to
I am trying to build a image capture for my iOS app, but I
I have been trying to implement this code , where i capture a image
I am using SnowFall app, in which a image is falling from top of
From my script/ directory in a rails app, I am trying to capture the
I'm trying to capture packets from my iPhone app. It does not use HTTP
I am trying to capture image and video from my android application with the
I am trying to allow HD capture from my iPad app via Apple's HDMI
I am trying to implement video capture in my app using AVFoundation. I have
I am trying to use Windows.Media.Capture.CameraCaptureUI() of Metro Style app in WPF application in

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.