在Android应用程序中将图像上载到服务器

在Android应用程序中将图像上载到服务器,android,camera,Android,Camera,在我的应用程序中,我正在捕获图像并将其上载到服务器。捕获和上传部分在我的500万像素安卓设备上运行良好 但拍照时,该应用程序偶尔会崩溃。我们注意到如果有人拿了 图片设置为高百万像素,照片无法上传,应用程序崩溃 如何减少800万像素照片的大小,以便能够在不崩溃的情况下上传? 我必须压缩捕获的图像吗。下面是我捕获图像的代码 ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE,fi

在我的应用程序中,我正在捕获图像并将其上载到服务器。捕获和上传部分在我的500万像素安卓设备上运行良好

但拍照时,该应用程序偶尔会崩溃。我们注意到如果有人拿了 图片设置为高百万像素,照片无法上传,应用程序崩溃

如何减少800万像素照片的大小,以便能够在不崩溃的情况下上传? 我必须压缩捕获的图像吗。下面是我捕获图像的代码

ContentValues values = new ContentValues();  
values.put(MediaStore.Images.Media.TITLE,fileName);  
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);          
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
intent.putExtra(MediaStore.ACTION_IMAGE_CAPTURE, capturedImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
startActivityForResult(intent, 3);
我正在上传OnActivity结果中的图像,如下所示

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
    String[] projection = { MediaStore.Images.Media.DATA};  
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst(); 
    capturedImage = cursor.getString(column_index_data);    
    String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
    new ProfileAddImageFileAsync().execute(url);
  }
}
     class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }        
            protected String doInBackground(String... Aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        IMAGE_RESPONSE = str;
                        ServerPost(str);
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;
            }
            protected void onProgressUpdate(String... Progress) 
            {
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
    }
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
      capturedImage = f;
      String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
      new ProfileAddImageFileAsync().execute(url);
  }
}
 class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

            protected String doInBackground(String... aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    Log.e("URl image uploading ",""+urlServer);

                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;
                    Log.e("maxBufferSize ",""+maxBufferSize);

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
                    Log.e("FileInput Stream in image upload ",""+fileInputStream);

                    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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        Log.e("ImageResponse ",""+str);
                        Appconstant.IMAGE_RESPONSE = str;
                        ProImgServerPost(str);
                        Log.e("ProImgServerPost ","added");
                        AddImgServerPost(str);
                        Log.e("AddImgServerPost ","added");
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;

            }

            protected void onProgressUpdate(String... progress) 
            {
                 Log.e("ANDRO_ASYNC",""+progress[0]);
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//               Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
//               ProfileImgPreview.setImageBitmap(bMap);
            }

            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
                Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
                ProfileImgPreview.setImageBitmap(bMap);
            }
    }
我正在运行一个进度对话框,直到上传完成,如下所示

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
    String[] projection = { MediaStore.Images.Media.DATA};  
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst(); 
    capturedImage = cursor.getString(column_index_data);    
    String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
    new ProfileAddImageFileAsync().execute(url);
  }
}
     class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }        
            protected String doInBackground(String... Aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        IMAGE_RESPONSE = str;
                        ServerPost(str);
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;
            }
            protected void onProgressUpdate(String... Progress) 
            {
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
    }
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
      capturedImage = f;
      String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
      new ProfileAddImageFileAsync().execute(url);
  }
}
 class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

            protected String doInBackground(String... aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    Log.e("URl image uploading ",""+urlServer);

                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;
                    Log.e("maxBufferSize ",""+maxBufferSize);

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
                    Log.e("FileInput Stream in image upload ",""+fileInputStream);

                    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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        Log.e("ImageResponse ",""+str);
                        Appconstant.IMAGE_RESPONSE = str;
                        ProImgServerPost(str);
                        Log.e("ProImgServerPost ","added");
                        AddImgServerPost(str);
                        Log.e("AddImgServerPost ","added");
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;

            }

            protected void onProgressUpdate(String... progress) 
            {
                 Log.e("ANDRO_ASYNC",""+progress[0]);
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//               Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
//               ProfileImgPreview.setImageBitmap(bMap);
            }

            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
                Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
                ProfileImgPreview.setImageBitmap(bMap);
            }
    }
class ProfileAddImageFileAsync扩展了AsyncTask
{                     
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
showDialog(对话框下载进度);
}        
受保护的字符串背景(字符串…Aurl)
{
HttpURLConnection=null;
DataOutputStream outputStream=null;
DataInputStream inStream=null;
尝试
{
URL urlServer=新URL(aurl[0]);
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
int字节读取,字节可用,缓冲区大小;
int maxBufferSize=1*1024*1024;
FileInputStream FileInputStream=newfileinputstream(新文件(capturedImage));
connection=(HttpURLConnection)urlServer.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
outputStream=新
DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(两个连字符+边界+行结束);
outputStream.writeBytes(“内容处置:表单数据;名称=\“用户文件\”文件名=\”“+capturedImage+“\”“+行结束”);
outputStream.writeBytes(lineEnd);
bytesAvailable=fileInputStream.available();
Log.e(“字节可用”,“字节可用”+);
bufferSize=Math.min(字节可用,maxBufferSize);
Log.e(“bufferSize”,即“+bufferSize”);
字节[]缓冲区=新字节[bufferSize];
Log.e(“bufer”和“+buffer”);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
而(字节读取>0)
{
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(两个连字符+边界+两个连字符+lineEnd);
@抑制警告(“未使用”)
int serverResponseCode=connection.getResponseCode();
@抑制警告(“未使用”)
字符串serverResponseMessage=connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
捕获(例外情况除外)
{
Log.e(“SD卡图像上传错误:”,“+ex.getMessage());
}
尝试
{
inStream=newdatainputstream(connection.getInputStream());
字符串str;
while((str=inStream.readLine())!=null)
{
图像_响应=str;
ServerPost(str);
}
流内关闭();
}
捕获(IOException ioex)
{
Log.e(“SD卡doFile上载错误:”,“+ioex.getMessage());
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度)
{
dismissDialog(对话框下载进度);
}
受保护的void onPostExecute(字符串未使用)
{
dismissDialog(对话框下载进度);
}
}

朋友们,请帮助我,上面的代码是我在5MP相机中的工作代码。

我得到了这样的解决方案,我用它来捕获和压缩图像

下面是我的相机部分

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
             file = new File(Environment.getExternalStorageDirectory(),  String.valueOf(System.currentTimeMillis()) + ".jpg"); 
             Log.e("ffffffffffiiiiiiiiilllllllllle ",""+file);
             f = String.valueOf(file);
             mCapturedImageURI = Uri.fromFile(file);
             Log.e("outputFileUri ",""+mCapturedImageURI);
             setupImage(intent);
             intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
             startActivityForResult(intent, 3); 
我正在上传OnActivity结果中的图像,如下所示

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
    String[] projection = { MediaStore.Images.Media.DATA};  
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst(); 
    capturedImage = cursor.getString(column_index_data);    
    String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
    new ProfileAddImageFileAsync().execute(url);
  }
}
     class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }        
            protected String doInBackground(String... Aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        IMAGE_RESPONSE = str;
                        ServerPost(str);
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;
            }
            protected void onProgressUpdate(String... Progress) 
            {
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
    }
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
      capturedImage = f;
      String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
      new ProfileAddImageFileAsync().execute(url);
  }
}
 class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

            protected String doInBackground(String... aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    Log.e("URl image uploading ",""+urlServer);

                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;
                    Log.e("maxBufferSize ",""+maxBufferSize);

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
                    Log.e("FileInput Stream in image upload ",""+fileInputStream);

                    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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        Log.e("ImageResponse ",""+str);
                        Appconstant.IMAGE_RESPONSE = str;
                        ProImgServerPost(str);
                        Log.e("ProImgServerPost ","added");
                        AddImgServerPost(str);
                        Log.e("AddImgServerPost ","added");
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;

            }

            protected void onProgressUpdate(String... progress) 
            {
                 Log.e("ANDRO_ASYNC",""+progress[0]);
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//               Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
//               ProfileImgPreview.setImageBitmap(bMap);
            }

            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
                Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
                ProfileImgPreview.setImageBitmap(bMap);
            }
    }
我正在运行一个进度对话框,直到上传完成,如下所示

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
    String[] projection = { MediaStore.Images.Media.DATA};  
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst(); 
    capturedImage = cursor.getString(column_index_data);    
    String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
    new ProfileAddImageFileAsync().execute(url);
  }
}
     class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }        
            protected String doInBackground(String... Aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        IMAGE_RESPONSE = str;
                        ServerPost(str);
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;
            }
            protected void onProgressUpdate(String... Progress) 
            {
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
    }
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 3) 
  {     
      capturedImage = f;
      String url = "http://xxxxxxxxxxxxxx.com/device_upload.php";
      new ProfileAddImageFileAsync().execute(url);
  }
}
 class ProfileAddImageFileAsync extends AsyncTask<String, String, String> 
      {                     
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

            protected String doInBackground(String... aurl) 
            {
                HttpURLConnection connection = null;
                DataOutputStream outputStream = null;
                DataInputStream inStream = null;
                try
                {
                    URL urlServer = new URL(aurl[0]);
                    Log.e("URl image uploading ",""+urlServer);

                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary =  "*****";
                    int bytesRead, bytesAvailable, bufferSize;
                    int maxBufferSize = 1*1024*1024;
                    Log.e("maxBufferSize ",""+maxBufferSize);

                    FileInputStream fileInputStream = new FileInputStream(new File(capturedImage) );
                    connection = (HttpURLConnection) urlServer.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
                    Log.e("FileInput Stream in image upload ",""+fileInputStream);

                    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=\"" + capturedImage +"\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    Log.e("bytesAvailable ",""+bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    Log.e("bufferSize ",""+bufferSize);

                    byte[] buffer = new byte[bufferSize];
                    Log.e("bufer ",""+buffer);
                    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);

                    @SuppressWarnings("unused")
                    int serverResponseCode = connection.getResponseCode();

                    @SuppressWarnings("unused")
                    String serverResponseMessage = connection.getResponseMessage();       

                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
                }
                catch (Exception ex)
                {
                    Log.e("SD Card image upload error: ","" + ex.getMessage());
                }
                try 
                {
                    inStream = new DataInputStream ( connection.getInputStream() );
                    String str;
                    while (( str = inStream.readLine()) != null)
                    {
                        Log.e("ImageResponse ",""+str);
                        Appconstant.IMAGE_RESPONSE = str;
                        ProImgServerPost(str);
                        Log.e("ProImgServerPost ","added");
                        AddImgServerPost(str);
                        Log.e("AddImgServerPost ","added");
                    }
                    inStream.close();
                }
                catch (IOException ioex)
                {
                    Log.e("SD card doFile upload error: ","" + ioex.getMessage());      
                }
                return null;

            }

            protected void onProgressUpdate(String... progress) 
            {
                 Log.e("ANDRO_ASYNC",""+progress[0]);
                 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//               Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
//               ProfileImgPreview.setImageBitmap(bMap);
            }

            protected void onPostExecute(String unused) 
            {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
                Bitmap bMap = BitmapFactory.decodeFile(capturedImage);
                ProfileImgPreview.setImageBitmap(bMap);
            }
    }
class ProfileAddImageFileAsync扩展了AsyncTask
{                     
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
showDialog(对话框下载进度);
}
受保护的字符串背景(字符串…aurl)
{
HttpURLConnection=null;
DataOutputStream outputStream=null;
DataInputStream inStream=null;
尝试
{
URL urlServer=新URL(aurl[0]);
Log.e(“URl图像上传”,“URl+urlServer”);