Estoy trabajando en una aplicación que pueda leer un archivo CSV y almacene los valores en una matriz para después procesarlos (es importante que el usuario pueda escoger el archivo, por lo que debe estar almacenado en el dispositivo).
Mi problema es que recibo un error del tipo java.io.FileNotFoundException: /storage/emulated/0/HeartDisease.csv (No such file or directory)
aunque esa ruta si exista, aun así con el método file.getName()
me devuelve 5049 y el file.getAbsolutePath()
devuelve /document/5049
Tengo el siguiente bloque de código para seleccionar el archivo CSV
public void clickButton() {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, CHOOSE_FILE);
} else {
Intent getFile = new Intent(Intent.ACTION_GET_CONTENT);
getFile.addCategory(Intent.CATEGORY_OPENABLE);
getFile.setType("text/csv");
startActivityForResult(Intent.createChooser(getFile, "Open CSV"), CHOOSE_FILE);
}
}
Y la siguiente función para leerlo (por el momento no eh intentado guardar la información porque no lo eh podido leer el CSV)
private void importCSV(File file) {
viewPath.setText(file.getAbsolutePath() + " is selected ");
try {
CSVReader reader = new CSVReader(new FileReader(file.getAbsolutePath()));
String[] line;
while ((line = reader.readNext()) != null) {
viewInfo.setText(line[0]);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "We can\'t read this file", Toast.LENGTH_SHORT).show();
}
}
Por ultimo mando a llamar la función importCSV
desde onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CHOOSE_FILE:
if (resultCode == RESULT_OK) {
importCSV(new File(data.getData().getEncodedPath()));
viewInfo.setText(data.getData().getLastPathSegment());
}
}
}
Este es el log completo
12/21 20:51:11: Launching 'app' on motorola moto g(6) plus.
$ adb shell am start -n "com.example.clusterdroid/com.example.clusterdroid.Home" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 21383 on device 'motorola-moto_g_6__plus-ZY322NHN9G'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/le.clusterdroi: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/le.clusterdroi: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/OpenGLRenderer: Skia GL Pipeline
I/Adreno: QUALCOMM build : 9128d71, Ib33d3e8e13
Build Date : 11/02/18
OpenGL ES Shader Compiler Version: EV031.25.03.01
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
Build Config : S L 6.0.7 AArch64
I/Adreno: PFP: 0x005ff112, ME: 0x005ff066
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/HeartDisease.csv (No such file or directory)
W/System.err: at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:231)
at java.io.FileInputStream.<init>(FileInputStream.java:165)
at java.io.FileInputStream.<init>(FileInputStream.java:112)
W/System.err: at java.io.FileReader.<init>(FileReader.java:58)
at com.example.clusterdroid.Home.importCSV(Home.java:83)
at com.example.clusterdroid.Home.onActivityResult(Home.java:72)
at android.app.Activity.dispatchActivityResult(Activity.java:7472)
W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4590)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4639)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
Process 21383 terminated.
/storage/emulated/0/HeartDisease.csv
es correcta, un saludo.