更新时间:2023-09-28 09:44:40
下面是从这是一个code段教程,这显示了选秀文件意图的实现:
Here's a code snippet from this tutorial, that shows the pick file Intent implementation:
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == PICK_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
Uri uri = intent.getData();
String type = intent.getType();
LogHelper.i(TAG,"Pick completed: "+ uri + " "+type);
if (uri != null)
{
String path = uri.toString();
if (path.toLowerCase().startsWith("file://"))
{
// Selected file/directory path is below
path = (new File(URI.create(path))).getAbsolutePath();
}
}
}
else LogHelper.i(TAG,"Back from pick with cancel status");
}
}
正如你所看到的,你的 onActivityResult()
方法返回的意图
,其中包含的文件路径,可以使用 intent.getData()
法来提取。然后你只需创建一个使用该路径的File对象,并使用 file.getAbsolutePath()
方法得到它的绝对路径。希望这有助于。
As you can see, your onActivityResult()
method returns you the Intent
, which contains the file path, that can be extracted using intent.getData()
method. Then you just create a File object using this path, and get the absolute path of it using file.getAbsolutePath()
method. Hope this helps.