我想将文件从移动存储(内部和外部存储)上传到服务器。通过使用文件选择器,我得到了FileNotFoundException
。这是我的代码 fragment :
public void openFileSystem(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select file to upload."), SELECT_FILE);
}
这是我的 onActivityResult()
和 getPath()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_FILE && resultCode == Activity.RESULT_OK) {
Uri path = data.getData();
String url = data.getData().getPath();
File file = new File(url);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
错误日志:
05-07 06:41:03.087: W/System.err(2195): java.io.FileNotFoundException: /document/2: open failed: ENOENT (No such file or directory)
05-07 06:41:03.097: W/System.err(2195): at libcore.io.IoBridge.open(IoBridge.java:409)
05-07 06:41:03.097: W/System.err(2195): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-07 06:41:03.097: W/System.err(2195): at com.gems.ComposeBulletin.onActivityResult(ComposeBulletin.java:121)
05-07 06:41:03.117: W/System.err(2195): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
05-07 06:41:03.117: W/System.err(2195): at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
05-07 06:41:03.127: W/System.err(2195): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
05-07 06:41:03.137: W/System.err(2195): at android.app.ActivityThread.access$1300(ActivityThread.java:135)
05-07 06:41:03.137: W/System.err(2195): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-07 06:41:03.137: W/System.err(2195): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 06:41:03.157: W/System.err(2195): at android.os.Looper.loop(Looper.java:136)
05-07 06:41:03.157: W/System.err(2195): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 06:41:03.167: W/System.err(2195): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 06:41:03.167: W/System.err(2195): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 06:41:03.177: W/System.err(2195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 06:41:03.187: W/System.err(2195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 06:41:03.187: W/System.err(2195): at dalvik.system.NativeStart.main(Native Method)
05-07 06:41:03.197: W/System.err(2195): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-07 06:41:03.217: W/System.err(2195): at libcore.io.Posix.open(Native Method)
05-07 06:41:03.217: W/System.err(2195): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-07 06:41:03.227: W/System.err(2195): at libcore.io.IoBridge.open(IoBridge.java:393)
所以我的座右铭是从内部存储或外部存储中选择任何文件(image
、pdf
、doc
)并将其上传到服务器。任何帮助将不胜感激!
最佳答案
这对于 ACTION_GET_CONTENT
来说并不可靠,因为不要求 Uri
返回指向您可以访问的文件。该文件可能位于应用程序的内部存储中,或者位于可移动媒体上,甚至位于云中的某个位置。这感觉像是本周的第六次,而且绝对是过去十分钟内的第二次...... a Uri
is not a file .
通过 ContentResolver
上的方法(如 openInputStream()
和 getType()
)使用设计的 Uri
。
关于java - Android 文件选择器中的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099196/