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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:10:04+00:00 2026-06-06T21:10:04+00:00

I made a program which uploads a video to an asp.net server. Then I

  • 0

I made a program which uploads a video to an asp.net server. Then I want to develop my application by adding a progress bar. However, after adding the progress bar I am not able to upload the file to the server.( this is the result that I get after debugging!!!) Could you please help me what is wrong with my code?

package com.isoft.uploader2;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Proje2Activity extends Activity
{
 @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                openGaleryVideo();
            }
        });

    }
/** Called when the activity is first created. */
public static final int SELECT_VIDEO=1;
public static final String TAG="UploadActivity";
String path="";

//Gallery'i aç
public void openGaleryVideo()
{
    Intent intent=new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Video"),SELECT_VIDEO);
}

//Dosyayı seç ve yükle
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_VIDEO) 
        {
            Uri videoUri = data.getData();
            path= getPath(videoUri);
            upload a = new upload();
            a.onPreExecute();
            a.doInBackground();
            a.onProgressUpdate();


        }
    }
}

//SD carddan yerini al
public String getPath(Uri uri)
{   
    String[] projection = { MediaStore.Video.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
public class upload extends AsyncTask<Object, Integer, Void> 
{
     public ProgressDialog dialog;
     File file=new File(path);  
     String urlServer = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
     String filename=file.getName();
     int bytesRead, bytesAvailable, bufferSize;
     byte[] buffer;
     int maxBufferSize = 20*1024*1024;
    @Override
    public void onPreExecute() 
    {
         dialog = new ProgressDialog(Proje2Activity.this);
         dialog.setMessage("Uploading...");
         dialog.setIndeterminate(false);
         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         dialog.setProgress(0);
         dialog.show();
            //Burada işlemi yapmadan önce ilk olarak ne yaptırmak istiyorsak burada yaparız.
            //Örneğin burada dialog gösterip "onPostExecute()" metodunda dismiss edebiliriz.
    }

    @Override
    public Void doInBackground(Object... arg0) 
    {
        // TODO Auto-generated method stub
        try
        {
        FileInputStream fileInputStream = new FileInputStream(file);

        URL url = new URL(urlServer);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setFixedLengthStreamingMode((int) file.length());

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

        // Enable POST method
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",  "multipart/form-data");
        connection.setRequestProperty("SD-FileName", filename);//This will be the file name
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        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();
            publishProgress();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }//end of while statement
        fileInputStream.close();
        publishProgress(100); 
        outputStream.flush();
        outputStream.close();
        }//end of try body
        catch (Exception ex)
        {
            //ex.printStackTrace();
            Log.e("Error: ", ex.getMessage());
        }
        return null;
     }//end of doInBackground method
     @Override
     public void onProgressUpdate(Integer... values) 
     {
       // TODO Auto-generated method stub
       dialog.setProgress((int) ((file.length()-bytesRead)/100));
     }//end of onProgressUpdate method
}// end of asyncTask class 
}//end of main
  • 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-06T21:10:06+00:00Added an answer on June 6, 2026 at 9:10 pm

    You have to call the upload task like

    new Upload().execute();
    

    and you are not doing the right by calling asynTask like the way you did

    So you code just looks like the following

    Upload a = new Upload();
    a.execute 
    

    Instead of

    upload a = new upload();
                a.onPreExecute();
                a.doInBackground();
                a.onProgressUpdate();
    

    And if your code to upload the file is correct , this will work

    and if you want to update the progress bar use Handler class

    Handler handler = new Handler(){
        @Override 
        public void handleMessage(int what){
            mProgress.setProgress(mProgressStatus);
         }
    

    as described in ProgressBar widget

    And inside the doInBackground() method invoke the method publishProgress() as described here AsyncTask Docs

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

Sidebar

Related Questions

I made a program which i want to launch with some protocal for eg
I made a program that opens an application, sleeps the thread for 500ms then
I made a program which can generate primes. I want the program to write
I have an ASP.NET application, which has two references added: MapServer and GDAL. These
I'm working on program which rergisters and processes service calls. I'm working in asp.net
I've made a small program which has 2 buttons and each does certain thing.
I have made a front end for a program which test java (foo.class) binary
Problem: Have made a small mail program which works perfectly on my developer pc
How to tell python for example, I made a program, which opens a specific
i made a program which converts lower case to upper case a string.i know

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.