Getting /data/data/my.app/files path programmatically? - android

In my app, I've just successfully downloaded a file and I opened it like this:
OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);
With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...

you could do it by using context.getFilesDir() + "/filename.ext"

Related

Access the file from salesforce

I am using salesforce for my android app in backend.
and somewhere while using SOQL, I am getting response something like this :
Article_FileNames":"Template.doc"
Article_FileContentTypes":"application/msword
Article_FileBodys":"/services/data/v35.0/sobjects/Hub_Knowledge_Articlekav/xxxxxxxxxxx/Article_FileBodys
ArticleNumber":"000001010
LastPublishedDate":"2015-12-09T20:34:48.000+0000
Now i am not getting, how I can let the user open the file named "Template.doc" on click.
How to open this file ?
is there any file path or id which will linkify this filename ?
Any help will be appreciated!
The only file types that are directly available from outside of your Salesforce instance are images, which are made so by setting the Externally Available Image flag on the Document object itself.
If you need to work with other file types, you're going to have to build them yourself to facilitate their download by the end user. This can be done by querying your instance for the Document, which will contain the Base64 encoded file contents (Body), that array's length (BodyLength), the file extension (Type), and also the file name itself (Name).
Given what you provided, your SOQL query would look something like :
SELECT Body, BodyLength, Name, Type FROM Document WHERE Name = 'Template' AND Type = 'doc'
From there, you should have everything you need to build the file in memory.

What is the best approach to open a TXT file from assets and read a string?

I need to check if a txt file exists on assets folder, and if exists I need to read the text of it and put into a string.
I saw some approaches on Google to open files from assets but I can't find one that gives you the possibility to check if the file exists and then read a string from it (because is a text resource).
You can just handle the exception properly when the file isn't found like this :
try {
InputStream is = SplashscreenActivity.this.getAssets().open("yourTextFile.txt");
} catch (IOException e) {
// catch your exception properly when the file isn't found
}
In short, you must already suppose to know if the file exists in assets or not. Because assets folder is packed up with files once you build your .apk file and can never be changed.
So, its pretty lame to verify if the file exists or not. It will only contain those files which you have added yourself.
More over, if you want to use R file to help you locate the file name, then simply put your files in a folder called raw. Its an alternate to assets but the files saved in this folder are referenced in the R class file - making it easier to get hold of the resource.

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

editing a text file packed in android apk

i am using a properties file to get urls to call web services in my app. Is there a way to edit that through some android activity.
The properties file is in src/resources folder. i am accessing that file using Resourcebundle.
You cant edit any file in application pkg file, however you can have following options to achieve this:
Have Preferences, if when you fetched preferences are null, load preferences with that property file, else let it be as it is.
Edit values in preferences.
If you still insist to use a text file only, copy property file to filesystem, and edit that file whatever you want to edit.

Sorting Images on Android

I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.

Categories

Resources