J'ai un scénario simple, je dois sélectionner un fichier de stockage interne ou externe et télécharger vers un serveur en utilisant le service.Impossible d'obtenir le fichier à partir de uri
fichier Ouverture chooser de cette manière
case R.id.action_cv: {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, null), Constant.REQUEST_CODE_OPEN);
break;
}
Dans onActivityResult()
j'obtenir le fichier
if (requestCode == Constant.REQUEST_CODE_OPEN) {
if (resultCode == RESULT_OK && null != data) {
Uri fileUri = data.getData();
if (Utils.validateFileType(Utils.getMimeType(UserProfileActivity.this, fileUri))){
Log.e("File", fileUri.toString());
uploadFile(fileUri.toString());
} else {
FutureSmileToast.getInstance()
.showToast(getApplicationContext(), "File type is not supported", SuperToast.Background.RED);
}
}
}
Il montre
E/File: content://com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt
Maintenant, la question est dans la méthode uploadFile()
qui est
private void uploadFile(String filePath) {
File file = new File(filePath);
String fileNameString = System.currentTimeMillis() + file.getName();
// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("cvfile", fileNameString, requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
Call<ServerResponse> call = mRestManager.getApiService().uploadCVFile(fileToUpload, filename);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if (null != response.body()) {
serverResponse = response.body();
if (serverResponse.getSuccess()) {
//Further proccesing
} else {
FutureSmileToast.getInstance()
.showToast(getApplicationContext(), serverResponse.getMessage(), SuperToast.Background.RED);
}
} else {
//
}
dialog.dismiss();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
t.printStackTrace();
FutureSmileToast.getInstance()
.showToast(getApplicationContext(), "Failed to upload CV", SuperToast.Background.RED);
}
});
} // CV upload
Cette méthode me donne Exception
08-11 10:46:35.819 27328-27328/pk.futuresmile W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt: open failed: ENOENT (No such file or directory)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okio.Okio.source(Okio.java:166)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.RequestBody$3.writeTo(RequestBody.java:117)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:189)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: at java.lang.Thread.run(Thread.java:818)
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err: at libcore.io.Posix.open(Native Method)
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err: at libcore.io.IoBridge.open(IoBridge.java:482)
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err: ... 14 more
Comment puis-je obtenir le fichier de URI et résoudre le problème qui fait exception?
essayer https://stackoverflow.com/a/41520090/1548824 – akhilesh0707
avant fichier télécharger sur le serveur vous devez obtenir le chemin du fichier à partir de URI – akhilesh0707
@AkhileshPatil oui! et j'ai essayé la solution que vous avez suggéré dans le commentaire ci-dessus il me donne l'erreur 'java.io.FileNotFoundException: /storage/emulated/0/some_file.txt: ouvert a échoué: ENOENT (aucun tel dossier ou répertoire)' –