上传图片到Android应用程序服务器上传图片、应用程序、服务器、Android

2023-09-07 00:19:27 作者:深碍过

在我的应用程序,我捕捉图像,并将其上传到服务器。在拍摄和上传的部分工作正常,在我的Andr​​oid设备这是500万像素。

In my app I am capturing an image and uploading it to the server. The capturing and uploading part works fine in my Android device which is of 5 mega pixel.

但拍照时,应用程序崩溃偶尔为之。如果有人需要我们注意到 图像,其具有高像素设置,相片不上载和应用程序崩溃。

But the app crashes occasionally when taking a photo. We've noticed if someone takes a picture that has a high megapixel setting, the photo does not upload and the app crashes.

如何减少的800万像素照片的大小要能上传没有崩溃? 我一定要COM preSS所拍摄的图像。以下是我的code,以捕捉图像

How to reduce the size of a 8 megapixel photo to be able to upload without crashing? Do I have to compress the captured image. Following is my code to capture an image

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结果的图像

I am uploading the image in OnActivity Result as follows

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);
  }
}

和我正在一个进度对话框,直到上传得到完成,如下:

And I am running a progress dialog box until the upload get completed, as follows

     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);
            }
    }

请帮我的朋友,以上code是我的工作code。在我的500万像素的摄像头。

Please help me friends, the above code is my working code in my 5MP camera.

推荐答案

我得到了这样的解决方案,我用它来采集和COM preSS的形象。

i got solution like this, i use to capture and compress the image.

以下是我的相机部分

 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结果的图像

i am uploading the image in OnActivity Result as follows

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);
  }
}

和我运行一个进程对话框,直到上传得到完成,如下:

And i am running an progress dialog box until the upload get completed, as follows

 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);
            }
    }