2011-09-08 17 views
1

私のアプリでは、画像をキャプチャしてサーバーにアップロードしています。キャプチャとアップロードの部分は、5メガピクセルの私のAndroidデバイスで正常に動作します。Androidアプリのサーバーに画像をアップロードする

写真を撮るときにアプリがクラッシュすることがあります。高画質設定のある の写真を誰かが受け取った場合、写真がアップロードされず、アプリがクラッシュすることがありました。

クラッシュせずにアップロードできるように8メガピクセルの写真のサイズを小さくするにはどうすればよいですか? キャプチャした画像を圧縮する必要がありますか。続いて

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結果に画像をアップロード午前

は、次のようにアップロードが完了しますまで、私は、進行状況ダイアログボックスを実行していた画像

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

を捕捉するために私のコードです

 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); 
      } 
    } 
関連する問題