我正在尝试使用API
和okHttp3
将很多图像发送到retrofit2
。我尝试从一台设备发送图像(一次发送5张图像),效果很好。然后我尝试同时从5个设备发送5个图像,并且效果很好,但是当我尝试从5个设备发送30个图像时,出现以下错误:
2020-01-17 14:57:07.416 32244-32264/my.example.com.puri W/zygote: Throwing OutOfMemoryError "Failed to allocate a 102554120 byte allocation with 8388608 free bytes and 97MB until OOM, max allowed footprint 174912000, growth limit 268435456"
2020-01-17 14:57:07.422 32244-32264/my.example.com.puri E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: my.example.com.puri, PID: 32244
java.lang.OutOfMemoryError: Failed to allocate a 102554120 byte allocation with 8388608 free bytes and 97MB until OOM, max allowed footprint 174912000, growth limit 268435456
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:178)
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:209)
at okio.Buffer.readString(Buffer.java:620)
at okio.Buffer.readString(Buffer.java:603)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:198)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
此error
不在第一个发送请求的设备上,而是到达第三个或最后一个设备。我首先单击send
按钮的设备总是成功。
我也对Manifest
进行了这些更改。
<application
android:hardwareAccelerated="false"
android:largeHeap="true"
>
这是我发出请求的方式。
@Multipart
@POST("installationdocument")
Call<Void> createNewDocument(@Header(AUTH_HEADER) String authorization, @Part ArrayList<MultipartBody.Part> images, @Part("document") RequestBody json);
和:
@Override
public void callRestApi(PuriRestApi restApi, RestApiJobService.ApiRequestor apiRequestor) {
MediaType JSON = MediaType.parse("application/json");
MediaType JPG = MediaType.parse("image/jpg");
ArrayList<MultipartBody.Part> imageParts = new ArrayList<>();
for (String imagePath : imagePaths) {
File image = new File(imagePath);
RequestBody imageBody = RequestBody.create(JPG, image);
imageParts.add(MultipartBody.Part.createFormData("images", image.getName(), imageBody));
}
RequestBody jsonPart = RequestBody.create(JSON, documentString);
apiRequestor.request(restApi.createNewDocument(authentication, imageParts, jsonPart), response -> {
if (response.isSuccessful()) {
if (imagePaths != null && imagePaths.length > 0) {
for (String imagePath : imagePaths) {
File image = new File(imagePath);
image.delete();
}
}
return true;
} else {
return false;
}
});
}
也许创建一个大变量就是引发此错误的原因。因为变量ArrayList<MultipartBody.Part> imageParts
将包含约150mb的图像数据和30张图像。但是除此之外,我不知道是什么原因导致此错误。
真的很感谢任何帮助。
在清单文件android:hardwareAccelerated =“ false”中添加以下实体,android:largeHeap =“ true”,它将在某些环境下工作。
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">