Saya menggunakan com.squareup.retrofit2:retrofit:2.2.0 dalam aplikasi saya untuk panggilan API. Saya juga menggunakan io.socket:socket.io-client:0.8.3 untuk koneksi Socket. Sekarang saya mendapatkan kesalahan berikut Fatal Exception: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed pada Kain. Saya tidak tahu di mana kesalahan ini terjadi,

Apakah masalah ini karena mengunggah gambar besar? Saya mengirim gambar base64 (lebar "1024" atau tinggi sebagai "1024") sebagai RequestBody multi-bagian.

Ini contoh panggilan Api Sampel saya dengan Gambar,

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

Menggunakan panggilan Api di atas saya mengunggah nama, foto profil, gambar depan dan belakang Lisensi,

       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
SHIDHIN.T.S 24 November 2017, 07:25

1 menjawab

Jawaban Terbaik

Saya mengatasinya dengan mengubah ukuran dengan memanggil transform() di 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();
            }
        }
    }
0
SHIDHIN.T.S 29 November 2017, 09:11