Итак, во-первых, приложение Android имеет установленные разрешения и библиотеки, не говоря уже о том, что на PHP-сервере обновлен файл php.ini, позволяющий загружать файлы.
Идея состоит в том, чтобы ввести путь к файлу для функции
public String postImage (String imagePath) {
result = ""+ System.currentTimeMillis();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "result")
.addFormDataPart("image", result + ".png",
RequestBody.create(MEDIA_TYPE_PNG, new File(imagePath)))
.build();
Request request = new Request.Builder()
.url("http://192.168.0.17/addImage.php")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
result = response.body().string();
}
}
});
return result;
}
Затем на сервере есть этот файл, который получает POST функции под названием «addImage.php»
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}?>
Но по какой-то причине я получаю эту ошибку
W/System.err: java.io.FileNotFoundException: content:/com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F77/ORIGINAL/NONE/294934663 (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:231)
at java.io.FileInputStream.<init>(FileInputStream.java:165)
at okio.Okio.source(Okio.kt:194)
at okhttp3.RequestBody$Companion$toRequestBody$3.writeTo(RequestBody.kt:162)
at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.kt:156)
at okhttp3.MultipartBody.writeTo(MultipartBody.kt:92)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:59)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
D/EGL_emulation: eglMakeCurrent: 0xe82c59a0: ver 3 1 (tinfo 0xe0ab13a0)
D/EGL_emulation: eglMakeCurrent: 0xe82c59a0: ver 3 1 (tinfo 0xe0ab13a0)
D/EGL_emulation: eglMakeCurrent: 0xe82c59a0: ver 3 1 (tinfo 0xe0ab13a0)
D/EGL_emulation: eglMakeCurrent: 0xe82c59a0: ver 3 1 (tinfo 0xe0ab13a0)
Application terminated.
Ошибка говорит о том, что указан путь к изображению (No such file or directory)
, однако я легко выбираю его и отображаю на Glide.
Android Studio также сообщает мне, что RequestBody.create () также устарела, однако страницы рецептов OkHttp показывают, что он используется.
Редактировать: Чтобы уточнить, ошибка не исходит от PHP, однако это происходит из OkHttp в Java или FileInputStream, однако я не понимаю, почему
Edit2: я осознал, что ошибка заключается в том, что указанный путь к изображению не был рабочим для распознавания File (), однако без ошибок я не могу передать файлы на сервер. Любая помощь по этому поводу?