2011-09-08 6 views
1

내 앱에서 이미지를 캡처하여 서버에 업로드하고 있습니다. 캡처 및 업로드 부분은 5 메가 픽셀 인 안드로이드 장치에서 잘 작동합니다.Android 앱의 서버에 이미지 업로드

사진을 찍을 때 앱이 가끔 충돌합니다. 사람들이 메가 픽셀 설정이 높은 사진을 찍으면 사진이 업로드되지 않고 앱이 다운되는 것을 발견했습니다.

충돌없이 업로드 할 수있는 8 메가 픽셀 사진의 크기를 줄이는 방법은 무엇입니까? 캡처 한 이미지를 압축해야합니까? 다음은 이미지

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

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

을 다음과 업로드가 완료 될 때까지

다음과 같이 내가 진행 대화 상자를 실행하고 나는이으로 onActivity 결과에 이미지를 업로드하고를 캡처하는 내 코드입니다
 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); 

그리고 난이 완료 얻을 업로드까지 진행 대화 상자를 실행하고

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

을 다음과 같이 전으로 onActivity 결과에 이미지를 업로드하고 있습니다

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