我想使用我得到的FileNotFoundException文件选择器将文件从移动存储(内部和外部存储)上传到server.By。下面是我的代码片段:
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)并将其上传到服务器上。任何帮助都将不胜感激!
发布于 2015-05-07 19:41:51
这在ACTION_GET_CONTENT中是不可靠的,因为不要求Uri返回指向您可以访问的文件。文件可能在应用程序的内部存储上,或者在可移动介质上,甚至在云中的某个地方。感觉这周已经是第六次了,绝对是过去十分钟内的第二次...a Uri is not a file。
通过openInputStream()和getType()等ContentResolver上的方法,按照设计使用Uri。
发布于 2015-05-07 20:03:03
试试这个。
Browse = (ImageView)findViewById(R.id.browsehoro);
Browse.setOnClickListener(this);
public void onClick(View v)
{
switch(v.getId())
{
case R.id.browsehoro:
img.setEnabled(true);
Intent intent = new Intent(Horoscope.this, FilePickerHoroscope.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
/* if (requestCode == REQUEST_PICK_FILE) {
Uri selectedImageUri = data.getData();
selectedFile = getPath(selectedImageUri);
// Print imagepath in textview here
filePath.setText(selectedFile);
System.out.println(requestCode);
System.out.println("Image Path : " + selectedFile);
img.setImageURI(selectedImageUri);
}*/
switch(requestCode)
{
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePickerHoroscope.EXTRA_FILE_PATH))
{
selectedFile = new String
(data.getStringExtra(FilePickerHoroscope.EXTRA_FILE_PATH));
filePath.setText(selectedFile.toString());
}
break;
}
}
}发布于 2015-05-07 19:35:47
String url = data.getData().getPath();
File file = new File(url);字符串url包含内容提供者路径。在这种情况下,正如您在logcat /document/2中看到的那样。File类无法处理此类内容提供程序路径。
您首先必须将该路径转换为实际的文件系统路径。
谷歌搜索'getRealPathFromUri‘。
https://stackoverflow.com/questions/30099196
复制相似问题