I put my files in the directory assets. how do I get them?
I tried following the instructions but have not worked:
File f = new File("/data/data/ant.BrowserX/files/" + pagCorrente + ".html");
File f = new File("file:///android_asset/" + pagCorrente + ".html");
there are no errors but running I can not access the file. How do I access the directory "assets"?
EDIT
thanks for the trick. I have the problem that I use a BufferedReader to read the file, and initialize it so, and not with an InputStream.
in = new BufferedReader(new FileReader(f));
how could I do?
You can get an InputStream from the file through the getAssets() method inside getResources():
InputStream is = getResources().getAssets().open("file.html");
Then just convert the stream to whatever you need
I know you can use this to get a inputstream :
final AssetManager assetMgr = this.getResources().getAssets();
InputStream is = assetMgr.open(pagCorrente +".html");
So probably you can use:
File f = new File(this.getResources().getAssets() + pagCorrente + ".html");
Simple answer, you don't access the "directory" assets. There is no directory structure in an .apk, it's just data. The framework knows how to access it (getAssets()), but you can't use it like a standard directory.
That being said, if you are trying to use a WebView and access a html file (which is what it looks like) they put a "fake" path reference in that you can use like this:
WebView wv = (WebView)this.findViewById(R.id.yourWebView);
wv.loadUrl("file:///android_asset/" + pagCorrente + ".html");
Related
I have this code:
try {
URL url = new URL (strURL);
input = url.openStream();
byte[] buffer = new byte[1500];
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
and I get this error:
do not hardcode /sd card/ use environment.getexternalstoragedirectory().getpath() instead
in
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
I have read about that:Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour
So the question is what will be final code when I replace it?
sd folder name different in Some phones
In samsung phones, it is named external_sd , and your code will fail.
control+shift+o --> to add imports in eclipse,see this link
"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code
The problem was that you call a file called Environment.java itself, so Eclipse didn't give me the choice to import Environment change that one.
Your revised code should look like
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");
You should be careful as not all devices have a /sdcard/ path.
Devices across the years have changed and this may not always contain a link to the proper external storage directory.
I want to create an inputstream for a file that is stored in the Downloads folder of my phone.
Earlier I had the required file as an asset and I read it with this code:
InputStream fin;
fin = getAssets().open(FILENAME);
However now I want to be able to open a file stored in my downloads folder /mnt/sdcard/Download as an InputStream NOT as a FileInputStream Object. I am using a library which requires an InputStream only as an argument so I can not use the FileInputStream
methods because I get a fatal exception if I do and the App stops.
How do I go about this? I tried to find some helper function from the documentation, but could not.
Edit: I also tried editing the code to this-
String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));
However still when I try to read the file, the App crashes.
I had the same issue as yours :
AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Here I used to access file1.xls from the assets folder.
I fixed it by using :
File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls"); // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Don't forget to get the storage permission first to access the storage.
Android provides many options for storing data persistently on the device. I have opted for internal storage, so please don't make suggestions for storing on an SD card. (I've seen too many questions about internal storage have answers for SD cards!!)
I would like to create subdirectories in my application's internal storage directory. I followed this SO answer, reproduced below.
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
Unfortunately, that's creating subdirectories with "app_" prefixes. So, from the example, the subdirectory looks like "app_mydir" (not ideal).
The answer to this SO question suggests that you can get rid of the "app_" prefix this way:
m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/mydir");
But I want to write a zip to something like /data/data/com.mypackages/files/mydir/the.zip.
So, in the end, my code looks like this:
File appDir = new File(getApplicationContext().getFilesDir() + "");
File subDir = new File(appDir + "/subDir");
File outFile = new File(subDir, "/creative.zip");
But, this is creating another error: "File does not exist" when I try this:
FileOutputStream fileStream = new FileOutputStream(outFile);
How can I (1) create a subdirectory without the "app_" prefix and (2) write a zip into it?
Also, if my first demand isn't reasonable, tell me why in the comments! I'm sure "app_" prefix has some meaning that escapes me atm.
Did you create the directories?
File appDir = getApplicationContext().getFilesDir();
File subDir = new File(appDir, "subDir");
if( !subDir.exists() )
subDir.mkdir();
File outFile = new File(subDir, "creative.zip");
Note that you shouldn't use / anywhere in your app, just in case it changes. If you do want to create your own paths, use File.separator.
I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?
Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());
You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)
I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));
When creating an Android application, I have some files that needs to be stored on the android itself.
How do I do this?
If you have local files, like some error tones, some openning video.. then place it in you assets folder of your project.
If you have dynamic data need to download at run time then use this guide.
Best place for generic files would be the assets folder.
You can access files through the AssetManager, which you can get with Activity.getAssets() for example.
Here is an example how you could access a text file:
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(getAssets().open("sometextfile.txt")));
// do stuff with br
} catch (IOException e) {
e.printStackTrace();
}
For more information on AssetManager read the Java Doc. Oh and yes, you can create folders in assets.
If you want to keep some files like readme.txt or even music files, you can use the raw folder inside of res folder. So create a folder named raw inside of res folder.
Inside of raw folder, let us assume that there is a file named readme.txt, assuming that the Activity class is called MyActivity.
Now, you can read the contents of a file into a String as shown below:
StringBuilder strContents = new StringBuilder();
String thisLine;
InputStream is = MyActivity.this.getResources().openRawResource(R.raw.readme);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((thisLine = br.readLine()) != null) { // while loop begins here
strContents.append(thisLine);
}
br.close();
//Now you have the data in strContents
Alternately, assets is also one such folder that you can use since the raw folder contains the file as is without any optimization, zipping done by Android.
So create an assets folder in your Project root folder and place your files there e.g. myfile.
Now, you can get an instance of the file InputStream as given below:
InputStream is = getBaseContext().getAssets().open("mydb");