java.lang.OutOfMemoryError:pthread_create(1040KB堆栈)失败:在okhttp3.ConnectionPool.put

时间:2017-11-24 04:25:43

标签: android out-of-memory okhttp okhttp3

我在我的API调用应用程序中使用com.squareup.retrofit2:retrofit:2.2.0我也使用io.socket:socket.io-client:0.8.3进行Socket连接。现在我在Fabric上收到错误Fatal Exception: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed。我不知道这个错误发生在哪里,

这个问题是由于上传了大图片吗?我发送base64图像(宽度" 1024"或高度为" 1024")作为多部分RequestBody。

这是我的带有Image的示例Api调用,

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

使用上述Api通话我正在上传姓名,个人资料照片,许可证正面和背面图像,

       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)

1 个答案:

答案 0 :(得分:0)

我通过调用transform()上的onActivityResult()

来调整大小
// 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();
            }
        }
    }
相关问题