gpt4 book ai didi

android - 如何从文本文件中获取 URI?

转载 作者:行者123 更新时间:2023-11-29 01:01:10 27 4
gpt4 key购买 nike

我这样做是为了获取并显示图像的路径,但我想知道如何对文本文件执行相同的操作。

val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
intent.type = "image/*"
startActivityForResult(intent, SELECT_IMAGE)

最佳答案

我觉得你可以试试

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);

获取uri

 @Override
protected void onActivityResult(..., Intent data) {
//do your validation

switch (requestCode) {
case PICKFILE_RESULT_CODE:
...
Uri uri = data.getData();
Log.d(TAG, "File Uri: " + uri.toString());
try {
Log.d(TAG, "File path: " + getPath(this, uri));
} catch (URISyntaxException e) {
e.printStackTrace();
}
//do your work
...
break;
}
}

添加工具

public static String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {"_data"};
Cursor cursor = null;

try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

关于android - 如何从文本文件中获取 URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51054458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com