Sorry for my English. I try to upload large video file on the server, this file more than 50 mb. When i upload small files, all works fine. In the manifest i set android:largeHeap="true"
and android:hardwareAccelerated="false"
its not help me. Below is my code:
Interface
@Multipart
@POST("/api/upload")
Observable<UploadVideoResponse> uploadVideo(@Header("Authorization") String userToken,
@Part MultipartBody.Part video);
method return file:
public static MultipartBody.Part getMultipartVideoBody(String pathVideo) {
try {
File videoFile = getFile(pathVideo);
RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), videoFile);
String fileName = videoFile.getName();
return MultipartBody.Part.createFormData("video", fileName, videoBody);
}catch (Exception e){
Log.e("File ", e.getMessage());
return null;
}
}
method upload on server
new DefaultApi().uploadVideo(
Utils.getHeaderToken(),
Utils.getMultipartVideoBody(pathVideo)
).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(videoView::showLoading)
.doOnTerminate(videoView::hideLoading)
.subscribe(data -> {
videoView.setData(data);
}
, throwable -> {
}
);
}
ERROR:
Throwing OutOfMemoryError "Failed to allocate a 100390006 byte allocation with 16777216 free bytes and 92MB until OOM"
FULL stacktrace
09-11 03:53:00.571 6724-6976/des.brain.ga E/art: Throwing OutOfMemoryError "Failed to allocate a 100390006 byte allocation with 16777216 free bytes and 92MB until OOM" 09-11 03:53:00.573 6724-6976/des.brain.ga E/AndroidRuntime: FATAL EXCEPTION: RxIoScheduler-3 Process: des.brain.ga, PID: 6724 java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread. at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.OutOfMemoryError: Failed to allocate a 100390006 byte allocation with 16777216 free bytes and 92MB until OOM at java.lang.String.(String.java:332) at java.lang.String.(String.java:371) at okio.Buffer.readString(Buffer.java:620) at okio.Buffer.readString(Buffer.java:603) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:199) at des.brain.ga.api.base.LoggingInterceptor.intercept(LoggingInterceptor.java:31) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) at okhttp3.RealCall.execute(RealCall.java:77) at retrofit2.OkHttpCall.execute(OkHttpCall.java:180) at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:40) at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24) at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:36) at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:28) at rx.Observable.unsafeSubscribe(Observable.java:10256) at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.unsafeSubscribe(Observable.java:10256) at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100) at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) 09-11 03:53:02.159 6724-6724/des.brain.ga E/WindowManager: android.view.WindowLeaked: Activity des.brain.ga.ui.main.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2ccb16a1 V.ED.... R.....ID 0,0-144,144} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:363) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:298) at com.kaopiz.kprogresshud.KProgressHUD.show(KProgressHUD.java:233) at des.brain.ga.ui.video.UploadVideoFragment.showLoading(UploadVideoFragment.java:150) at des.brain.ga.ui.video.VideoPresenter$$Lambda$4.call(Unknown Source) at rx.internal.operators.OperatorDoOnSubscribe.call(OperatorDoOnSubscribe.java:40) at rx.internal.operators.OperatorDoOnSubscribe.call(OperatorDoOnSubscribe.java:27) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:44) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.unsafeSubscribe(Observable.java:10256) at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) at rx.Observable.subscribe(Observable.java:10352) at rx.Observable.subscribe(Observable.java:10319) at rx.Observable.subscribe(Observable.java:10159) at des.brain.ga.ui.video.VideoPresenter.uploadVideo(VideoPresenter.java:65) at des.brain.ga.ui.video.UploadVideoFragment.lambda$uploadVideo$0$UploadVideoFragment(UploadVideoFragment.java:127) at des.brain.ga.ui.video.UploadVideoFragment$$Lambda$0.onClick(Unknown Source) at com.afollestad.materialdialogs.MaterialDialog.onClick(MaterialDialog.java:410) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
LoggingInterceptor
public final class LoggingInterceptor implements Interceptor {
private final Interceptor mLoggingInterceptor;
private LoggingInterceptor() {
mLoggingInterceptor = new HttpLoggingInterceptor()
//.setLogLevel(RestAdapter.LogLevel.FULL)
.setLevel(BuildConfig.DEBUG ? Level.BODY : Level.NONE);
}
@NonNull
public static Interceptor create() {
return new LoggingInterceptor();
}
@Override
public Response intercept(Chain chain) throws IOException {
return mLoggingInterceptor.intercept(chain);
}
}
and i use LoggingInterceptor
like this:
return new OkHttpClient.Builder()
.connectTimeout(300, TimeUnit.SECONDS)
.addInterceptor(LoggingInterceptor.create())
.addInterceptor(logging)
.build();
I have faced same error while uploading large video using retrofit,but
RemovingloggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
or set is as NONE
worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With