1

I have a path stored in my database to a file text1.txt, which contains the text I want to display. The file is in the assets folder, assets/text1.txt.

How do I open this file and display its content?

The code is:

if (placetext != null) {
    try 
    {
        InputStream textpath = getAssets().open(text);
        //Bitmap bit = BitmapFactory.decodeStream(textpath);
        placetext.setText(text);
        //placetext.setText(text);
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

...and the view on the emulator is just text1.txt not the content of the file.

i already have the solution

String text12 = b.getString("texts") try { InputStream is = getAssets().open(text12); // int size = is.available();

           byte[] buffer = new byte[size];
           is.read(buffer);
           is.close();

           String text= new String(buffer);

           placetext = (TextView)findViewById(R.id.detailText2);
           placetext.setText(text);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }

2 Answers2

0

That's normal because :

InputStream textpath = getAssets().open(text);

I guess here : text represent the name of your file to open.

placetext.setText(text); 

That put the text passed in parameter in the textfield, so right now the name of your file.

To put the content of your file in the textfield, you have to open the file, read it and store the content in a StringBuffer and after put the StringBuffer content in the textfield.

EDIT :

StringBuilder text = new StringBuilder();
Scanner scanner = new Scanner(new FileInputStream(new File('yourfile')));
    try {
      while (scanner.hasNextLine()){
      text.append(scanner.nextLine());
      }
    }
    finally{
        scanner.close();
    }

}

One solution among a lot of others to read the content of a file in Java.

Hope it helps

Jeremy D
  • 4,787
  • 1
  • 31
  • 38
  • thank for your response; InputStream textpath = getAssets().open(text), text represent field on sqlite, which contain path. it's work with image file and image path, but doesnt work on text.txt –  Dec 10 '11 at 01:01
  • Ok could you explain more precisely the problem. As far as I understand, you want to get the content of a file stored in the asset folder of your app, and displaythe content of it in a textfield? right? – Jeremy D Dec 10 '11 at 01:23
  • Look at that : http://stackoverflow.com/questions/4789325/android-path-to-asset-txt-file – Jeremy D Dec 10 '11 at 01:29
  • i do with images refer thislink, http://stackoverflow.com/questions/6292696/how-can-i-set-image-view-which-stored-in-file-system-according-to-path-in-databa and it works.. so, i try with text, and it doesnt work.. –  Dec 10 '11 at 23:33
0

This is what I use to read the contents of a XML stored in /assets folder:

 public static String getXMLFromAssets(Context ctx, String pathToXML){   
        InputStream rawInput;
        //create a output stream to write the buffer into  
        ByteArrayOutputStream rawOutput = null;
        try {
            rawInput = ctx.getAssets().open(pathToXML);
            //create a buffer that has the same size as the InputStream  
            byte[] buffer = new byte[rawInput.available()];  
            //read the text file as a stream, into the buffer  
            rawInput.read(buffer);  
            rawOutput = new ByteArrayOutputStream();  
            //write this buffer to the output stream  
            rawOutput.write(buffer);  
            //Close the Input and Output streams  
            rawOutput.close();  
            rawInput.close();
        } catch (IOException e) {
            Log.e("Error", e.toString());
        }
        //return the output stream as a String  
        return rawOutput.toString();  
}
Sgali
  • 362
  • 2
  • 12