java如何在Android中从URI/URI打开文件
我正试图从模拟器的内部存储器中打开一个文件(试图将其放入SD卡中,但将其放入内部存储器?)。我让用户选择一个文件,然后通过intent将该文件的Uri(注意大小写)传递给文本编辑活动。在这次活动中,我尝试了一些东西。我当前的代码是这样的(请原谅我的疯狂,我已经尝试了很多东西):
try {
Intent intent = getIntent();
File file;
Uri uri = Parcels.unwrap(intent.getParcelableExtra("uri"));
URI newUri = new URI("file://"+uri.toString());
Log.d("uri", uri.toString());
Log.d("newUri", newUri.toString());
file = new File(newUri);
if (intent != null) {
Log.d("uriPath", uri.toString());
if (uri != null) {
file = new File(uri.getPath());
String text = readTextFile(file);
if (text != null) {
Log.d("in if text not null", text);
mEditText.setText(text);
}
// mEditText.setText(readTextFile(file));
}
}
}
catch(URISyntaxException e) { e.printStackTrace(); }
我尝试了很多事情,但都没有成功。我要么没有找到文件(因为它没有在文件前面加任何前缀,只是尝试加载“document/primary%3Aindex.html”),要么出现其他异常,当前:
06-06 05:43:18.826 10794-10794/net.a40two.pext E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.a40two.pext, PID: 10794
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.a40two.pext/net.a40two.pext.ui.EditorActivity}: java.lang.IllegalArgumentException: Found authority in URI: file://content://com.安卓.externalstorage.documents/document/primary%3Aindex.html
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at 安卓.app.ActivityThread.-wrap11(ActivityThread.java)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:148)
at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: Found authority in URI: file://content://com.安卓.externalstorage.documents/document/primary%3Aindex.html
at java.io.File.checkURI(File.java:230)
at java.io.File.<init>(File.java:175)
at net.a40two.pext.ui.EditorActivity.onCreate(EditorActivity.java:67)
at 安卓.app.Activity.performCreate(Activity.java:6237)
at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at 安卓.app.ActivityThread.-wrap11(ActivityThread.java)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:148)
at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
即使没有文件://连接,也会发生这种情况。我能在网上找到的大多数东西要么非常陈旧,要么都不起作用,要么两者兼而有之。我们衷心感谢您的帮助
我主要活动中获取此URI的代码直接来自开发人员。安卓com:
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all.
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i("onActivityResult", "Uri: " + uri.toString());
Intent intent = new Intent(MainActivity.this, EditorActivity.class);
intent.putExtra("uri", Parcels.wrap(uri));
startActivity(intent);
finish();
}
}
}
共 (0) 个答案