【发布时间】:2019-05-21 19:21:36
【问题描述】:
我尝试从 Android 设备获取文件的文件路径并使用 Intent 将其存储在 FTP 服务器上,文件对象是从我从 URI 模板获得的路径初始化的,但是当我将此文件对象发送到 FileInputStream 它给了我找不到文件的例外
Uri uri;
FileInputStream fis = null;
if (requestCode==6 && resultCode==RESULT_OK && data!=null)
{
uri=data.getData();//return URI of selected File
File fil= null;
try {
fil = new File(getRealPathFromURI(uri));
fis= new FileInputStream(fil);
client.storeFile(filePath,fis);
} catch (Exception e) {
e.getMessage();
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = NavigationDrawer.this.getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = 0;//cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
【问题讨论】:
-
uri和getRealPathFromURI的输出是什么样的?您可能根本无法获得绝对文件路径(您可能会发现这个blog post by commonsware useful) -
这是 Uri "content://com.android.providers.media.documents/document/image%3A13425" 的值,这是 getRealPathFromURI "image:13425" 的输出
标签: java android ftp filepath filenotfoundexception