Rokas Devolskis
Rokas Devolskis

Reputation: 43

Android openFileInput can't resolve file path

I added external library for file browsing. That library returns selected file path key, but not value.

package com.example.dev.nordugrid;
import com.orleonsoft.android.simplefilechooser.ui.FileChooserActivity;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.view.View;
import android.content.Intent;
import java.io.*;

public class jdlFailas extends ActionBarActivity {
final int FILE_CHOOSER = 1;
public String fileSelected;
public String myText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jdl_failas);
}

public void openFILE(View view) {
    Intent intent = new Intent(jdlFailas.this, FileChooserActivity.class);
    startActivityForResult(intent, FILE_CHOOSER);

    try {
        FileInputStream fin = new FileInputStream ( fileSelected);
        int c;
        String temp="";
        while( (c = fin.read()) != -1){
            temp = temp + Character.toString((char)c);
        }

        fin.close();
        myText = temp.toString();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
    }
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == FILE_CHOOSER) && (resultCode == RESULT_OK)) {
        fileSelected = data.getStringExtra(com.orleonsoft.android.simplefilechooser.Constants.KEY_FILE_SELECTED);

        TextView textView = (TextView) findViewById(R.id.editText2);

        Toast.makeText(this, R.string.pasirinktasFailas + myText, Toast.LENGTH_SHORT).show();
        textView.setText(myText);
    }
}

public void jdlSave(View view) {
    Intent intent = new Intent(jdlFailas.this, NaujaUzduotis.class);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_jdl_failas, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

If I put normal filename, program runs normal:

   FileInputStream fin = openFileInput("file.txt");

Anyway, my main question is "How can I work with files, if I want to use path not string?"

.xml file

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.dev.nordugrid.jdlFailas">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/pridekiteJDL"
    android:id="@+id/textView6"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/prideti"
    android:id="@+id/button12"
    android:onClick="openFILE"
    android:layout_below="@+id/textView6"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/rasytiRanka"
    android:id="@+id/textView7"
    android:layout_below="@+id/button12"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_below="@+id/textView7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ikelti"
    android:id="@+id/button13"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="jdlSave" />

There is LogCat code in pastebin link: http://pastebin.com/43kzEgRu

Upvotes: 2

Views: 7121

Answers (4)

Arjun Othayoth
Arjun Othayoth

Reputation: 451

If you have an rooted phone then the you will get the openFileInput() from below path

/data/data/package name/files/file name (ex config.txt)

Other wise check with your Device file explorer with the same path

Upvotes: 0

tsig
tsig

Reputation: 148

I had similar problem when file_name included a path:

InputStream inputStream = context.openFileInput(file_name);

Solution was to replace above line with following:

file_name=context.getFilesDir() + "/"+file_name;
InputStream inputStream = new FileInputStream(new File(file_name));

Upvotes: 3

Chris Stratton
Chris Stratton

Reputation: 40357

Your question is ultimately due to overlooking the critical difference between

FileInputStream fin = openFileInput("file.txt");

vs

FileInputStream fin = new FileInputStream("failas.txt")

The first is workable (within an Activity or Service), because openFileInput() is an Android-unique method which opens a file located in the private directory of your application.

The second is not workable on Android, because Java's FileInputStream(String path) constructor when given only a filename will attempt to open a file in the working directory, which on Android is the root directory of the device - a place where you app cannot have stored any data.

When you are dealing with the External Storage as your path "/storage/sdcard/uzduotis.txt" indicates, you cannot use openFileInput(), however in contrast to your assertion you can do the following:

String path = "/storage/sdcard/uzduotis.txt";
FileInputStream fin = new FileInputStream(path);

Or even literally hardcode it as

FileInputStream fin = new FileInputStream("/storage/sdcard/uzduotis.txt");

Of course overall success, as always, depends on all the usuals such as having a working program, having permission to the External Storage, and having previously created a file at that name and location.

Upvotes: 4

greenapps
greenapps

Reputation: 11214

public void openFILE(View view) {
Intent intent = new Intent(jdlFailas.this, FileChooserActivity.class);
startActivityForResult(intent, FILE_CHOOSER);

try {
    FileInputStream fin = new FileInputStream ( fileSelected);
    int c;
    String temp="";
    while( (c = fin.read()) != -1){
        temp = temp + Character.toString((char)c);
    }

    fin.close();
    myText = temp.toString();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
}
}

As said before: You cannot open the file right away as the user first has to select a file. So change to:

public void openFILE(View view) {
Intent intent = new Intent(jdlFailas.this, FileChooserActivity.class);
startActivityForResult(intent, FILE_CHOOSER);
}

And:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == FILE_CHOOSER) && (resultCode == RESULT_OK)) {
    fileSelected = data.getStringExtra(com.orleonsoft.android.simplefilechooser.Constants.KEY_FILE_SELECTED);
    Toast.makeText(this, fileSelected, Toast.LENGTH_SHORT).show();

try {
    FileInputStream fin = new FileInputStream ( fileSelected);
    int c;
    String temp="";
    while( (c = fin.read()) != -1){
        temp = temp + Character.toString((char)c);
    }

    fin.close();
    myText = temp.toString();
    TextView textView = (TextView) findViewById(R.id.editText2);

    Toast.makeText(this, R.string.pasirinktasFailas + myText, Toast.LENGTH_SHORT).show();
    textView.setText(myText);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    Toast.makeText(this, "FileNotFoundException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  } catch (IOException e) {
 e.printStackTrace();       
  Toast.makeText(this, "IOException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

Upvotes: 0

Related Questions