2017-11-24 1 views
1

Ich verwende com.squareup.retrofit2:retrofit:2.2.0 in meiner Anwendung für API-Aufruf Ich verwende auch io.socket:socket.io-client:0.8.3 für Socket-Verbindung. Jetzt bekomme ich folgenden Fehler Fatal Exception: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed auf Fabric. Ich weiß nicht, wo dieser Fehler passiert,java.lang.OutOfMemoryError: pthread_create (1040KB stack) fehlgeschlagen: bei okhttp3.ConnectionPool.put

ist dieses Problem aufgrund des Hochladens eines großen Bildes? Ich sende Base64 Bilder (Breite "1024" oder Höhe wie "1024") als Multipart RequestBody.

Hier ist mein Beispiel Api Anruf mit Bild,

RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), name); 

RequestBody profilePic = null; 
Bitmap bitmap = BitmapFactory.decodeFile(mProfilePicPath); 
if (bitmap != null) { 
    String base64Image = String.valueOf("data:image/png;base64," + encodeImage(bitmap).replaceAll(System.getProperty("line.separator"), "")); 
    profilePic = RequestBody.create(MediaType.parse("multipart/form-data"), base64Image); 
} 
RequestBody licenseFront = null; 
Bitmap bitmap = BitmapFactory.decodeFile(mCarDriveOnePath); 
if (bitmap != null) { 
    String base64Image = String.valueOf("data:image/png;base64," + encodeImage(bitmap).replaceAll(System.getProperty("line.separator"), "")); 
    licenseFront = RequestBody.create(MediaType.parse("multipart/form-data"), base64Image); 
} 
RequestBody licenseBack = null; 
Bitmap bitmap = BitmapFactory.decodeFile(mCarDriveTwoPath); 
if (bitmap != null) { 
    String base64Image = String.valueOf("data:image/png;base64," + encodeImage(bitmap).replaceAll(System.getProperty("line.separator"), "")); 
    licenseBack = RequestBody.create(MediaType.parse("multipart/form-data"), base64Image); 
} 

//API Call 
ApiClient.updateDriverDetails(nameBody , profilePic, licenseFront, licenseBack).enqueue(new Callback<UpdateDriverResponse>() { 
    @Override 
    public void onResponse(Call<UpdateDriverResponse> call, Response<UpdateDriverResponse> response) { 
     Log.d("UpdateDriver", "Success"); 
    //Here Comes the parsing of API response   
    } 

    @Override 
    public void onFailure(Call<UpdateDriverResponse> call, Throwable t) { 
     Log.d("UpdateDriver", "Error"); 
    } 
}); 

den oben API-Aufruf Ich Namen bin Hochladen profile pic, Lizenz Vorder- und Rückseite Bilder,

 Fatal Exception: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again 
     at java.lang.Thread.nativeCreate(Thread.java) 
     at java.lang.Thread.start(Thread.java:1063) 
     at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921) 
     at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339) 
     at okhttp3.ConnectionPool.put(ConnectionPool.java:149) 
     at okhttp3.OkHttpClient$1.put(OkHttpClient.java:158) 
     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192) 
     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) 
     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) 
     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179) 
     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129) 
     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
     at java.lang.Thread.run(Thread.java:818) 
+0

Könnten Sie Ihren Code schreiben – sJy

+0

@sJy: Ich weiß nicht, wo dieser Fehler passiert. Ich habe diesen Fehler auf Fabric gefunden, den ich auf meiner Frage erwähnt habe. Welchen Code brauchst du? Auch dieser Fehler ist einmal passiert und es ist auf dem Samsung Galaxy A5. –

+0

Ihr Retrofit-Builder-Code zusammen mit einem API-Call-Code – sJy

Antwort

0

ich es gelöst durch Ändern der Größe von transform() auf onActivityResult() Aufruf

// This method is to resize image. 
public Bitmap transform(String image_file_path) { 
     Log.d(TAG, "Photo transform :: " + image_file_path); 
     Bitmap result = null; 
     try { 
      if (image_file_path != null && !image_file_path.isEmpty()) { 
       File image_file = new File(image_file_path); 
       if (image_file != null && image_file.exists()) { 

        // First decode with inJustDecodeBounds=true to check dimensions 
        final BitmapFactory.Options options = new BitmapFactory.Options(); 
        //options.inJustDecodeBounds = true; 
        options.inSampleSize = 5; 

        Bitmap source = BitmapFactory.decodeFile(image_file_path, options); 
        if (source != null) { 
         boolean isLandscape = source.getWidth() > source.getHeight(); 
         int newWidth, newHeight; 
         if (isLandscape) { 
          newWidth = 1024; 
          newHeight = Math.round(((float) newWidth/source.getWidth()) * source.getHeight()); 
         } else { 
          newHeight = 1024; 
          newWidth = Math.round(((float) newHeight/source.getHeight()) * source.getWidth()); 
         } 
         Log.d(TAG, "Resized : " + source.getWidth() + "x" + source.getHeight() + " to " + newWidth + "x" + newHeight); 

         result = Bitmap.createScaledBitmap(source, newWidth, newHeight, true); 
         //result = ImageResize.resize(image_file, newWidth, newHeight, ResizeMode.AUTOMATIC); 

         if (result != source) 
          source.recycle(); 

         writeImage(result, image_file.getPath()); 

         if (image_file_path == mCurrentCBTImagePath || image_file_path.equals(mCurrentCBTImagePath)) { 
          CbtEncodedString = encodeImage(result); 
          mPresenter.setCBTbaseEncodedString(CbtEncodedString); 
         } else if (image_file_path == mCurrentCourierImagePath || image_file_path.equals(mCurrentCourierImagePath)) { 
          CourierEncodedString = encodeImage(result); 
          mPresenter.setCourierbaseEncodedString(CourierEncodedString); 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      Log.e(TAG, "transform (Resize Image)::" + e.getMessage()); 
     } catch (OutOfMemoryError e) { 
      Log.e(TAG, "OutOfMemoryError: transform" + e.getMessage()); 
     } 
     return result; 
    } 


// This method is to save the resized image. 
    public static void writeImage(Bitmap bitmap, String path) { 
     FileOutputStream out = null; 
     try { 
      File imagePath = new File(path); 
      if (imagePath.exists()) 
       imagePath.delete(); 
      out = new FileOutputStream(imagePath); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (out != null) { 
        out.flush(); 
        out.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
Verwandte Themen