问题
When I click a load button, the file content does not load in TextView
There appears to be an error uploading the file in BufferedReader
in ReadText
method.
And this is a picture from "Logcat"
public class MainActivity extends AppCompatActivity
private static final int READ_REQUEST_CODE = 42;
private static final int PERMISSION_REQUEST_STORAGE = 1000;
Button b_load;
TextView tv_output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_STORAGE);
}
b_load = findViewById(R.id.b_load);
tv_output = findViewById(R.id.tv_output);
b_load.setOnClickListener(v -> performFileSearch());
}
private String readText(String input) {
File file = new File(Environment.getExternalStorageDirectory(), input);
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("\n");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return text.toString();
}
private void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
@SuppressLint("MissingSuperCall")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri uri = data.getData();
String path = uri.getPath();
path = path.substring(path.indexOf(":") + 1);
Toast.makeText(this, "" + path, Toast.LENGTH_SHORT).show();
tv_output.setText(readText(path));
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
2020-12-21 21:40:11.722 10679-10679/? I/downloadfilecs: Not late-enabling -Xcheck:jni (already on)
2020-12-21 21:40:11.860 10679-10679/? E/downloadfilecs: Unknown bits set in runtime_flags: 0x8000
2020-12-21 21:40:11.877 10679-10679/? W/downloadfilecs: Unexpected CPU variant for X86 using defaults: x86
2020-12-21 21:40:12.727 10679-10679/com.gsbitdz.downloadfilecsv W/RenderThread: type=1400 audit(0.0:321): avc: denied { write } for name="property_service" dev="tmpfs" ino=6875 scontext=u:r:untrusted_app:s0:c149,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.gsbitdz.downloadfilecsv
2020-12-21 21:40:12.737 10679-10708/com.gsbitdz.downloadfilecsv D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2020-12-21 21:40:12.737 10679-10708/com.gsbitdz.downloadfilecsv W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2020-12-21 21:40:12.880 10679-10679/com.gsbitdz.downloadfilecsv W/downloadfilecs: Verification of void androidx.fragment.app.FragmentActivity.<init>() took 135.663ms
2020-12-21 21:40:13.041 10679-10708/com.gsbitdz.downloadfilecsv D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-12-21 21:40:13.238 10679-10708/com.gsbitdz.downloadfilecsv D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-12-21 21:40:13.274 10679-10708/com.gsbitdz.downloadfilecsv D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-12-21 21:40:14.048 10679-10679/com.gsbitdz.downloadfilecsv W/downloadfilecs: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-12-21 21:40:14.050 10679-10679/com.gsbitdz.downloadfilecsv W/downloadfilecs: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-12-21 21:40:14.350 10679-10706/com.gsbitdz.downloadfilecsv D/HostConnection: HostConnection::get() New Host Connection established 0xe194e190, tid 10706
2020-12-21 21:40:14.392 10679-10706/com.gsbitdz.downloadfilecsv D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
2020-12-21 21:40:14.443 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglCreateContext: 0xe191a120: maj 3 min 0 rcv 3
2020-12-21 21:40:14.455 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
2020-12-21 21:40:14.557 10679-10706/com.gsbitdz.downloadfilecsv W/Gralloc3: mapper 3.x is not supported
2020-12-21 21:40:14.560 10679-10706/com.gsbitdz.downloadfilecsv D/HostConnection: createUnique: call
2020-12-21 21:40:14.561 10679-10706/com.gsbitdz.downloadfilecsv D/HostConnection: HostConnection::get() New Host Connection established 0xe194e2d0, tid 10706
2020-12-21 21:40:14.609 10679-10706/com.gsbitdz.downloadfilecsv D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
2020-12-21 21:40:14.610 10679-10706/com.gsbitdz.downloadfilecsv D/eglCodecCommon: allocate: Ask for block of size 0x1000
2020-12-21 21:40:14.610 10679-10706/com.gsbitdz.downloadfilecsv D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ff807000 size 0x2000
2020-12-21 21:40:14.652 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
2020-12-21 21:40:14.792 10679-10690/com.gsbitdz.downloadfilecsv I/downloadfilecs: NativeAlloc concurrent copying GC freed 2178(194KB) AllocSpace objects, 1(20KB) LOS objects, 51% free, 1449KB/2985KB, paused 1.088ms total 156.156ms
2020-12-21 21:40:15.694 10679-10706/com.gsbitdz.downloadfilecsv I/OpenGLRenderer: Davey! duration=1382ms; Flags=1, IntendedVsync=143238159428185, Vsync=143238159428185, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=143238165820360, AnimationStart=143238165943520, PerformTraversalsStart=143238166044870, DrawStart=143238477194280, SyncQueued=143238505988740, SyncStart=143238512527730, IssueDrawCommandsStart=143238512771480, SwapBuffers=143239375546860, FrameCompleted=143239548621940, DequeueBufferDuration=636000, QueueBufferDuration=1062000,
2020-12-21 21:40:16.126 10679-10679/com.gsbitdz.downloadfilecsv I/Choreographer: Skipped 92 frames! The application may be doing too much work on its main thread.
2020-12-21 21:40:16.394 10679-10706/com.gsbitdz.downloadfilecsv I/OpenGLRenderer: Davey! duration=1707ms; Flags=0, IntendedVsync=143238442663991, Vsync=143239975997263, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=143239981645120, AnimationStart=143239981761440, PerformTraversalsStart=143239982286150, DrawStart=143239988279900, SyncQueued=143239990141830, SyncStart=143240089030570, IssueDrawCommandsStart=143240089226000, SwapBuffers=143240092769820, FrameCompleted=143240248807880, DequeueBufferDuration=8247000, QueueBufferDuration=57494000,
2020-12-21 21:40:21.830 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
2020-12-21 21:40:34.810 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/amor.txt: open failed: EACCES (Permission denied)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:159)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at java.io.FileReader.<init>(FileReader.java:72)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at com.gsbitdz.downloadfilecsv.MainActivity.readText(MainActivity.java:57)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at com.gsbitdz.downloadfilecsv.MainActivity.onActivityResult(MainActivity.java:86)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:8110)
2020-12-21 21:40:34.811 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4838)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
2020-12-21 21:40:34.812 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.os.Looper.loop(Looper.java:214)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
2020-12-21 21:40:34.813 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.Linux.open(Native Method)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: at libcore.io.IoBridge.open(IoBridge.java:482)
2020-12-21 21:40:34.814 10679-10679/com.gsbitdz.downloadfilecsv W/System.err: ... 17 more
2020-12-21 21:40:35.473 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
2020-12-21 21:40:35.892 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
2020-12-21 21:40:35.964 10679-10706/com.gsbitdz.downloadfilecsv I/OpenGLRenderer: Davey! duration=1123ms; Flags=1, IntendedVsync=143258693907527, Vsync=143258760574191, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=143258765046080, AnimationStart=143258765134600, PerformTraversalsStart=143258765205160, DrawStart=143259742451060, SyncQueued=143259746669690, SyncStart=143259748996890, IssueDrawCommandsStart=143259749309920, SwapBuffers=143259752592880, FrameCompleted=143259819367860, DequeueBufferDuration=5693000, QueueBufferDuration=936000,
2020-12-21 21:40:36.191 10679-10706/com.gsbitdz.downloadfilecsv D/EGL_emulation: eglMakeCurrent: 0xe191a120: ver 3 0 (tinfo 0xe190f3a0)
来源:https://stackoverflow.com/questions/65398877/read-text-file-from-download-folder-failed