简体   繁体   中英

How to open a file in the assets folder using a path stored in the database?

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); } 

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

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();  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM