I want to get the application size (in bytes). The way I tried was as follows:
PackageStats ps = new PackageStats(packageInfo.packageName);
int size = ps.codeSize;
This returns 0 every time. (Bascially every value returned is 0 - cacheSize, dataSize etc)
I do not want to use reflection as mentioned here (as its a non documented way)
So is there any way to fetch this information ?
One definition of application size is the size of the apk file in bytes.
You can get that by doing new File() on the ApplicationInfo.sourceDir value for the application, and then doing a File.length() on that file.
Contrary to the sound of the class name, PackageStats.class doesn't access system on your phone and find the size of your app, data, and cache files (wish it did). It is used to pull this information (if available) from a parcel, or to take a package and write this information if provided by the package into a parcel.
Using zmarties approach, new File(), is a workable solution.
Related
I am building an application and I am thinking of how to "parametrise" all the strings in the application (is it even possible) in order to allow me to change them easily without "redeploying" it again ...
meaning it will be somewhere in a file with strings (something like you have PO files in PHP when using templates and different languages) where I can manage it ..
it might be useful when I would like to use different languages :)
I am kinda struggling on this one, so I was thinking if you can give me a clue or show me where to "go" to study how this should be implemented ..
Thanks
If you want different langage in your app, create as many strings.xml files as you need. However, when you add new strings file, you have to redeploy.
To avoid that, you should call a specific API in backend which send you all the texts according to the langage of the device. For that you must manage back and front.
So if I understand your question correcly go to res - >value folder -> strings.xml - > open it and you will see something like this:
<string name="app_name">this is your app name</string>
And now every time that you want text to be "this is your app name" all you need to do is to add this line:
android:text="#string/app_name"
And when you will change the actual string in strings.xml it will also change in every place he is being used (android:text="#string/app_name")
Android has a built-in mechanism for localising assets (Strings included)
https://developer.android.com/guide/topics/resources/localization#creating-alternatives
What you are trying to do is known as 'Localisation', this can be helpful if you want to give users different language support and similar kind of stuff.
In android, this is done by putting all the strings in the Strings.xml file located in the res folder.
If you're using android studio, just press Alt + Enter on any hardcoded string and then select 'Extract string resource' from the popup, give the name of the string and Voila! you're done.
It is also a part of good coding practice, in fact you've might have noticed 'Lint warnings' in your layout files if you type any hardcoded string, and it asks you to add this string in the strings.xml file
You can create a separate String file for each language that you wanna include.
Option 1
Using the Default Built-In Mechanism
You already have answers about this or you can read the official documentation about it.
Essentially, you maintain an xml file called strings.xml which is a key-value of Strings. Each file (one per language) will be located in a values folder. E.g.: for french, you'd want values-fr/strings.xml. Each file will include all the translated strings. If a value is not found in French, then the english file will be searched instead as a fallback. If the key is not there, the app will crash (if I am not mistaken).
Pros
Included with the system
Supports many languages
Packed at compile time, always available.
Cons
Resources are "read-only" once they are compiled; you cannot change a string and save the change this way.
Option 2
Roll your own Thing.
If you need to be able to dynamically alter these strings, you'll need a few key pieces:
A way to obtain/download said strings.
A default in case step 1 fails (what if the user cannot download them?) You need defaults.
To ensure every widget that needs to display text, calls your own creation of a class that can manage said dynamic strings (i'll elaborate down below)
You need to know what to do if a String is somehow magically missing; because this is dynamic, there has to be a fallback in case the string is not found (see 2)
This has to be relatively fast (you don't want expensive lookups when constructing strings and UI elements).
Pros
You can model this the way it works best for you
You will be able to load strings as you see fit and even change them at runtime.
Cons
You have to build all this.
It's error prone, and most likely slower than the native solution.
You must ensure you don't miss strings and that you have dafults.
You must modify normal widgets (say TextView) to understand how to fetch the strings (or you must always provide them), and this is not going to be automatic, you'll either have to delegate or subclass into a YourCustomTextViewThatUnderstandsYourStringThing extends TextView (name... is just a draft ;) )
You must ensure you don't leak memory by keeping strings in memory you don't care anymore.
If you want to "persist" these downloaded languages (and you should), you have to write/use your own persisting mechanism (either by writing the files or by using some database, shared preferences is not the place for these).
You need to cache them (see above) and manage the validity of the strings (what if they become old, can they become old? when should you re-fetch them?)
etc.
As you can see it's not trivial and each app has its own world of problems to solve, but that's roughly what it means.
As for "code"... the simplest way I can think of (or rather, the bare basics) are:
Find a way to "store" the strings: e.g.:
Map<String, String> oneLanguage
So in this Map, you store the KEY (to find the value) and the VALUE:
oneLanguage.put("app_name", "My Super App")
Keep all the strings in one place in memory:
Map<String, Map<String, String>> allLanguages
so then you do:
allLanguages.put("English", oneLanguage);
Now you can add other languages:
anotherLanguage.put("app_name", "Mi Super App"); //my Spanish is top-notch
allLanguages.put("Spanish", anotherLanguage);
Now you have a place where to store (in memory) all your keys/values for each language.
When you want to retrieve Spanish, you'd have a method like:
String getString(#NonNull String locale, #NonNull String key) {
return allLanguages.get(locale).get(key);
}
And then you'd need to (either manually or via subclassing or whatever approach you find more convenient) to set these strings like:
// I mean, don't hardcode the locale... but you get the idea.
appNameTextView.setText(yourLanguageManager.getString("Spanish", "app_name"));
What approach you take for this last step, is entirely up to you.
As usual, all the above is pseudo-code, and not a complete solution to this approach. Things you want to do: ask the device what locale is using, keep track of which locale is in use (so you don't have to pass it every time), fetch these locales from your server (not pictured) :), persist these to disk, as well as save in shared preferences, the "locale" key that the user has selected, add methods to your yourLanguageManager so it can have things like:
boolean updateLocale(String locale, Map<String, String newStrings)
boolean deleteLocale(String locale)
Map<String, String> getLocale(String locale)
etc.. you get the idea.
All in all, it's just a simple data structure that can contain keys and values.
Good luck!
I want to simulate a drive using the SKNavigationSettings.SKNavigationType.FILE. Is there an easy way to generate one of these files? I see the Seattle.log in the demo project, and I could just edit some coordinates and make my own however it would be great to simulate a real drive. Also I am not sure what all of the entries are:
"47.655942 -122.137419, 11.000000, 19.000000, 0.000000, 1380803959889470, 03.10.2013 15:39:19" (what are 11.000000, 19.000000, 0.000000?)
Update: I still do not have a way of doing this and I do not understand some of the values (listed above). The file is Seattle.log and it just consists of a bunch of lines like the one above separated by newlines.
You can find the full SDK documentation here.
https://www.developer.skobbler.com/docs/android/3.0.2/index.html
Yes, you can obtain a logging file with data collected from a real drive. You can use the startLoggingPositions(String filePath, SPositionLoggingType positionLoggingType) and public boolean stopLoggingPositions() from the SKPositionLoggingManager class (com.skobbler.ngx.positioner.logging package) to accomplish that. You will obtain a log file (if the positionLoggingType is set to SK_POSITION_LOGGING_TYPE_LOG) similar to Seattle.log at the specified path.
The values stored in the file are (in this order): Latitude, Longitude, Course, Speed, Accuracy, Timestamp.
So, I'm using a file sharing app on Android. It creates a duplicate copy which is uploaded to it's server.
PROBLEM
The following code works for a duplicate copy I manually create. That is, I long press and copy the file into the same directory with a File Manager. Then my function returns true. When it compares the duplicate image due to the app and the original image, I get false.
MD5-checksums are different so that is out of the options.
CODE
public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight()
* bitmap1.getRowBytes());
bitmap1.copyPixelsToBuffer(buffer1);
ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight()
* bitmap2.getRowBytes());
bitmap2.copyPixelsToBuffer(buffer2);
return Arrays.equals(buffer1.array(), buffer2.array());
}
Here are the images :
Original image -
Duplicate image created by the app -
My code currently returns false while comparing these two images.
How do I get the code to return true?
Your problem is due to artefacts created by JPEG compression, if you can always keep the images in PNG then your problem is most likely solved. If you can't do that, then you need a better algorithm to compare the images. This is exactly the same problem discussed at Comparing image in url to image in filesystem in python
For instance, running the algorithms mentioned in the earlier discussion, we get a similarity of more than 99%. With that similarity value, you can say the images are the same.
I tried to make a .map file to use it in my android application. My point is to show map on screen with help of mapsforge library. Library requires a .map file. The problem is generating it with osmosis mapfilewriter plugin. When I use command
./osmosis --rx file=/home/user/poland.osm --mw file=result.map bbox=51.09,16.9,51.14,17
I get
SEVERE: Thread for task 1-rx failed
java.lang.OutOfMemoryError: Java heap space
I tried using another order of arguments but it also doesn't work. I also tried using -Xmx1200m option to increase javas heap memory but usage of memory was this same and didn't help.
I would be grateful for help.
Use the parameter type=hd.
The type parameter has two options ram and hd. Although I don't see that the documentation doesn't explicitly state what hd means, I would guess it means "hard disk", meaning it would offload data to disk, rather than keeping it all in memory.
The resulting command would be then:
./osmosis --rx file=/home/user/poland.osm --mw file=result.map bbox=51.09,16.9,51.14,17 type=hd
If you have enough memory, you need to tell Java about it (it will only use a limited amount otherwise); but this is not an argument passed directly to osmosis.
On Windows, you can follow this advice given by Emilie Laffray on the OSM-dev list:
In osmosis.bat is the following line... REM # JAVACMD_OPTIONS - The
options to append to the java command, typically used to modify jvm
settings such as max memory.
so, either modify osmosis.bat or, create a new file called osmosis.bat
in the all users profile directory or your profile directory, to
include a 'set JAVACMD_OPTIONS = -Xmx1024M' line (to set maximum
memory usage to 1GB, if you have enough RAM)...
On Linux (and I'd assume Mac OS X also), you can create the file ~/.osmosis
containing the line
JAVACMD_OPTIONS=-Xmx1024M
which will be loaded by osmosis on startup. Note that both examples above use the value 1024M, i.e. you're giving Java access to 1024 MB of RAM - you could use a different value depending on your system.
I have a long series of graphics -- icon1_0.png, icon1_1.png, icon1_2.png..., icon12_0.png, icon12_1.png, icon12_2.png -- and I'd like to package them with my android application. Ideally I think I should be able to load them as resources but the resource id's are set up as java identifiers. Of course, java identifiers can't be assembled at runtime. I have to ask for R.drawable.icon12_00 so I cannot set up a loop
for(int icon=0;icon<12;icon++)
for(int frame=0;frame<3;frame++)
//syntax error obviously
BitmapFactory.decodeResource(getResources(), R.drawable."icon" + icon + "_" + frame + ".png");
So is there any way to get resources by their names? Better yet, is there a canonical way outside the resource system to pack data files into an android application package so that I can get at them?
I'm thinking about reflection but that doesn't seem like the right solution to me.
Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.
I know you've found an answer already, but if you use reflection then you will see a good speed increase, as getIdentifier() is slower. I wrote about how to do the reflection method here. However, this only works if you're accessing your own resources.
Reflection is also very slow, you should just create an array with all of your identifies in it.