I have a simple scenario, I have to select a file from Internal or external storage and upload to a Server using service.
Opening file chooser in this way
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;
}
In onActivityResult()
I get file
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);
}
}
}
It shows
E/File: content://com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt
Now the issue is in uploadFile()
method which is
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
This method gives me 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
How can I get file from URI and resolve the issue which causes exception?