シンプルなシナリオがあります。内部または外部のストレージからファイルを選択し、サービスを使用してサーバーにアップロードする必要があります。 onActivityResult()
でuriからファイルを取得できませんでした
開くファイル選択
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;
}
私は、ファイル
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);
}
}
}
を得ることが
E/File: content://com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt
さて問題は
ですuploadFile()
方法であることを示しています
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
この方法は
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
がどのようにURIからファイルを取得し、例外が発生し、問題を解決することができます私に例外を与えますか?
は、サーバーにアップロードファイルの前にhttps://stackoverflow.com/a/41520090/1548824 – akhilesh0707
これを試してみてくださいURIからファイルパスを取得する必要があります – akhilesh0707
@AkhileshPatilはい!上記のコメントで提案した解決策を試してみましたが、 'java.io.FileNotFoundException:/storage/emulated/0/some_file.txt:オープンに失敗しました:ENOENT(そのようなファイルやディレクトリはありません)' –