2011-09-08 173 views
1

在我的应用程序中,我捕捉图像并将其上传到服务器。捕获和上传部分工作正常,在我的Android设备是500万像素。将图像上传到Android应用程序的服务器

但是,应用程序偶尔在拍照时崩溃。我们注意到,如果某人拍摄了具有高百万像素设置的 照片,该照片不会上传,并且应用会崩溃。

如何减小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); 
      } 
    } 

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

+0

应用程序崩溃时的错误是什么?你没能得到任何细节? –

+0

对不起,我无法得到这些细节... –

+0

你可以看看ACRA。它可以帮助您解决这个http://code.google.com/p/acra/ –

回答

1

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

以下是我的相机部分

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) 
    {  
     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); 
      } 
    } 
相关问题