Can't use font in Android - android

I've read every SO post regarding use of fonts in Android and I still can't get mine to work. I'm getting the following exception:
java.lang.RuntimeException: native typeface cannot be made
Here's my code (this class and static method is taken from here):
public class SafeTypefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c) {
final String assetPath = "fonts/robotobold.ttf";
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
AssetManager assetManager = c.getAssets();
Typeface t = Typeface.createFromAsset(assetManager, assetPath); //Throws exception on this line!
cache.put(assetPath, t);
} catch (Exception e) {
L.p("Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
The font is in the assets/fonts folder:
(this is still Eclipse. I know, I know).
Things I've tried:
I've tried moving the font to the root of the assets folder and updating the assetPath.
I checked folder and file permissions.
I tried using the application context instead of the activity context I'm getting.
I tried using different font files (one send via email, another downloaded from the Internet).

As I was creating this question I ran into the solution. I've spend a few hours on this so I guess its worth keeping.
My problem was that I was working on a library project. According to this (Android Library assets folder doesn't get copied), assets in library projects aren't available to the main project, which is why the font file wasn't being found. (Again, this project is still using Eclipse. Possibly not the case using Gradle+AS).
The solution is to copy the fonts into the main project, and voila!

Related

Cross-device local assets location in Weex

Working with Weex 0.16 I generated the default "WeexDemo" project for Web, iOS and Android, and the default index files works without issue.
I'm trying to figure out the folder location where I can put image files and other assets. I changed the "logoUrl" from an http url to a relative path ('bg.png')
Serving on the browser over 8080 port web root is the project root folder, so if I copy bg.png to "dist" folder I can see it when logoUrl is "/dist/bg.png"
On the iOS project "/dist/bg.png" does not work, since the root seems to be the "dist" folder itself, if I change to logoUrl "bg.png", it works.
On the Android project, both "/dist/bg.png" or "bg.png" fail. I can see in the folder "/platforms/android/app/src/main/assets/dist" the bg.png file has been copied but I don't seem to able to access it.
Is there any solution where I can use the same folder to drop the assets and have it accessible for all devices?
Do as follows :
In your application class :
WXSDKEngine.registerComponent("yourImage", YourImage.class);
create class YourImage.java and it should look like this :
public class YourImage extends WXComponent<ImageView> {
public YourImage(WXSDKInstance instance, WXDomObject dom, WXVContainer parent) {
super(instance, dom, parent);
}
#Override
protected ImageView initComponentHostView(#NonNull Context context) {
ImageView imageView = new ImageView(context);
return imageView;
}
#WXComponentProp(name = Constants.Name.SRC)
public void setSrc(String src) {
if (src == null) {
return;
}
if(null != getHostView()) {
Picasso.with(WXEnvironment.getApplication()).load(src).into(getHostView());
}
}
}
In your vue/js file
<yourImage class="image" :src="url"> </yourImage>
In your WeexUIFragment/Activity : while loading page data through map, add one key and value as follows:
HashMap<String, Object> map = new HashMap<>();
map.add("image_url", "android local asset url")
map.put("PAGENAME", "");
mWXSDKInstance = new WXSDKInstance(YourCurrentActivity.this);
mWXSDKInstance.registerRenderListener(YourCurrentActivity.this);
mWXSDKInstance.render(pageName, weexJSUrl, map, null, WXRenderStrategy.APPEND_ASYNC);
In JS file in script/data block to retrieve asset local image url:
const url = _.get(weex,'config.image_url')
If you need more help: You can look into Component Extend section in below link :
https://weex.incubator.apache.org/guide/extend-android.html

Libgdx Choosing correct textures based on DPI?

How to implement a system which takes the best texture based on dpi from a set like in android SDK, because LibGDX is platform independent, and can't use the already existent one?
May be my solution is not the best but I used it in real project and it worked.
In your Game class use Gdx.graphics.getDensity() method to choose appropriate folder, store its name in public field and load your assets:
public class MyGame extends Game {
public static String folder;
private AssetManager assets;
#Override
public void create() {
if (Gdx.graphics.getDensity < 1) {
folder = "lowDpiImages/";
} else {
folder = "highDpiImages/";
}
...
assets = new AssetManager();
assets.load(folder + "image.png", Texture.class, paramsNearest);
...
}
Then in you other classes use folder name to get assets from AssetManager:
assets.get(MyGame.folder + "image.png", Texture.class);
You can write more sophisticated folder choosing algorithm of cause ;-)

Can't read lines from file by using StreamReader on Unity3d (Android)

I need to read a text stream by using StreamReader from file on android platform. File is about 100k lines, so even editor is getting stuck if i try to load it all to TextAsset or if i use WWW.
I simply need to read that file line by line without loading it all to a string. Then i'll do a tree generation from the lines that i got from the file. (But probably that part doesn't matter, i just need help on file reading part.)
I'm giving the code that i wrote down below. It works perfectly on editor, but fails on android.
I would be glad if anyone tell me, what am i missing.
(ps. english is not my native and this is my first question on the site. so sorry for the any mistakes that i may have done.)
private bool Load(string fileName)
{
try
{
string line;
string path = Application.streamingAssetsPath +"/";
StreamReader theReader = new StreamReader(path + fileName +".txt", Encoding.UTF8);
using (theReader)
{
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
exception = e.Message;
return false;
}
}
You can't use Application.streamingAssetsPath as a path on Android because streaming assets are stored within the JAR file with the application.
From http://docs.unity3d.com/Manual/StreamingAssets.html:
Note that on Android, the files are contained within a compressed .jar
file (which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file then you will need to use additional software to see
inside the .jar archive and obtain the file.
Use WWW like this in a coroutine:
WWW data = new WWW(Application.streamingAssetsPath + "/" + fileName);
yield return data;
if(string.IsNullOrEmpty(data.error))
{
content = data.text;
}
Or, if you really want to keep it simple (and your file is only a few 100k, stick it in a resource folder:
TextAsset txt = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
string content = txt.text;

How to provide data files for android unit tests

I am developing software that loads information from XML files using Android's implementation of java.xml.parsers.DocumentBuilder and DocumentBuilderFactory. I am writing unit tests of my objects and I need to be able to provide a variety of xml files that will exercise the code under test. I am using Eclipse and have a separate Android Test Project. I cannot find a way to put the test xml into the test project such that the code under test can open the files.
If I put the files in /assets of the test project, the code under test cannot see it.
If I put the files in the /assets of the code under test, it can of course see the files, but now I'm cluttering up my actual system with test only data files.
If I hand copy the files to the /sdcard/data directory, I can open them from the code under test, but that interferes with automating my tests.
Any suggestions of how to have different xml test files reside in the test package but be visible to the code under test would be greatly appreciated.
Here is how I tried to structure the unit test:
public class AppDescLoaderTest extends AndroidTestCase
{
private static final String SAMPLE_XML = "sample.xml";
private AppDescLoader m_appDescLoader;
private Application m_app;
protected void setUp() throws Exception
{
super.setUp();
m_app = new Application();
//call to system under test to load m_app using
//a sample xml file
m_appDescLoader = new AppDescLoader(m_app, SAMPLE_XML, getContext());
}
public void testLoad_ShouldPopulateDocument() throws Exception
{
m_appDescLoader.load();
}
}
This did not work as the SAMPLE_XML file is in the context of the test, but AndroidTestCase is providing a context for the system under test, which cannot see an asset from the test package.
This is the modified code that worked per answer given:
public class AppDescLoaderTest extends InstrumentationTestCase
{
...
protected void setUp() throws Exception
{
super.setUp();
m_app = new Application();
//call to system under test to load m_app using
//a sample xml file
m_appDescLoader = new AppDescLoader(m_app, SAMPLE_XML, getInstrumentation().getContext());
}
Option 1: Use InstrumentationTestCase
Suppose you got assets folder in both android project and test project, and you put the XML file in the assets folder. in your test code under test project, this will load xml from the android project assets folder:
getInstrumentation().getTargetContext().getResources().getAssets().open(testFile);
This will load xml from the test project assets folder:
getInstrumentation().getContext().getResources().getAssets().open(testFile);
Option 2: Use ClassLoader
In your test project, if the assets folder is added to project build path (which was automatically done by ADT plugin before version r14), you can load file from res or assets directory (i.e. directories under project build path) without Context:
String file = "assets/sample.xml";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
For Android and JVM unit tests I use following:
public final class DataStub {
private static final String BASE_PATH = resolveBasePath(); // e.g. "./mymodule/src/test/resources/";
private static String resolveBasePath() {
final String path = "./mymodule/src/test/resources/";
if (Arrays.asList(new File("./").list()).contains("mymodule")) {
return path; // version for call unit tests from Android Studio
}
return "../" + path; // version for call unit tests from terminal './gradlew test'
}
private DataStub() {
//no instances
}
/**
* Reads file content and returns string.
* #throws IOException
*/
public static String readFile(#Nonnull final String path) throws IOException {
final StringBuilder sb = new StringBuilder();
String strLine;
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))) {
while ((strLine = reader.readLine()) != null) {
sb.append(strLine);
}
} catch (final IOException ignore) {
//ignore
}
return sb.toString();
}
}
All raw files I put into next path: ".../project_root/mymodule/src/test/resources/"
Try this for Kotlin:
val json = File("src\\main\\assets\\alphabets\\alphabets.json").bufferedReader().use { it.readText() }

Copy the shared preferences XML file from /data on Samsung device failed

There's an exporting feature in my application. It's just a copy operation since all my settings are store in shared preference.
I just copy the xml file from /data/data/package.name/shared_prefs/settings.xml to SD card. It works fine on my HTC desire. However, it might not work on Samsung devices, and i got the following error while I try to copy the file.
I/System.out( 3166): /data/data/package.name/shared_prefs/settings.xml (No such file or directory)
in the directory.
Anyone know how to fix it, or is there another simple way to store the shared preference ?
Thanks.
Never never never never never never never never never hardwire paths.
Unfortunately, there's no getSharedPreferenceDir() anywhere that I can think of. The best solution I can think of will be:
new File(getFilesDir(), "../shared_prefs")
This way if a device manufacturer elects to change partition names, you are covered.
Try this and see if it helps.
CommonsWare's suggestion would a be clever hack, but unfortunately it won't work.
Samsung does not always put the shared_prefs directory in the same parent directory as the getFilesDir().
I'd recommend testing for the existence of (hardcode it, except for package name):
/dbdata/databases/<package_name>/shared_prefs/package.name_preferences.xml and if it exists use it, otherwise fall back to either CommonsWare's suggestion of new File(getFilesDir(), "../shared_prefs") or just /data/data/<package_name>/shared_prefs/package.name_preferences.xml.
A warning though that this method could potentially have problems if a user switched from a Samsung rom to a custom rom without wiping, as the /dbdata/databases file might be unused but still exist.
More details
On some Samsung devices, such as the Galaxy S series running froyo, the setup is this:
/data/data/<package_name>/(lib|files|databases)
Sometimes there's a shared_prefs there too, but it's just Samsung's attempt to confuse you! Don't trust it! (I think it can happen as a left over from a 2.1 upgrade to 2.2, but it might be a left over from users switching roms. I don't really know, I just have both included in my app's bug report interface and sometimes see both files).
And:
/dbdata/databases/<package_name>/shared_prefs
That's the real shared_prefs directory.
However on the Galaxy Tab on Froyo, it's weird. Generally you have: /data/data/<package_name>/(lib|shared_prefs|files|databases)
With no /dbdata/databases/<package_name> directory, but it seems the system apps do have:
/dbdata/databases/<package_name>/yourdatabase.db
And added bonus is that /dbdata/databases/<package_name> is not removed when your app is uninstalled. Good luck using SharedPreferences if the user ever reinstalls your app!
Try using
context.getFilesDir().getParentFile().getAbsolutePath()
Best way to get valid path on all devices - run method Context.getSharedPrefsFile defined as:
/**
* {#hide}
* Return the full path to the shared prefs file for the given prefs group name.
*
* <p>Note: this is not generally useful for applications, since they should
* not be directly accessing the file system.
*/
public abstract File getSharedPrefsFile(String name);
Because of it hidden need use reflection and use fallback on fail:
private File getSharedPrefsFile(String name) {
Context context = ...;
File file = null;
try {
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = context.getClass().getMethod("getSharedPreferencesPath", new Class[] {String.class});
file = (File)m.invoke(context, new Object[]{name});
} catch (Throwable e) {
Log.w("App TAG", "Failed call getSharedPreferencesPath", e);
}
}
if (file == null) {
Method m = context.getClass().getMethod("getSharedPrefsFile", new Class[] {String.class});
file = (File)m.invoke(context, new Object[]{name});
}
} catch (Throwable e) {
Log.w("App TAG", "Failed call getSharedPrefsFile", e);
file = new File(context.getFilesDir(), "../shared_prefs/" + name + ".xml");
}
return file;
}
On some Samsungs implements like this:
public File getSharedPrefsFile(String paramString) {
return makeFilename(getPreferencesDir(), paramString + ".xml");
}
private File getPreferencesDir() {
synchronized (this.mSync) {
if (this.mPreferencesDir == null) {
this.mPreferencesDir = new File("/dbdata/databases/" + getPackageName() + "/", "shared_prefs");
}
File localFile = this.mPreferencesDir;
return localFile;
}
}
On other Android like this:
public File getSharedPrefsFile(String name) {
return makeFilename(getPreferencesDir(), name + ".xml");
}
private File getPreferencesDir() {
synchronized (mSync) {
if (mPreferencesDir == null) {
mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
}
return mPreferencesDir;
}
}
private File getDataDirFile() {
if (mPackageInfo != null) {
return mPackageInfo.getDataDirFile();
}
throw new RuntimeException("Not supported in system context");
}
After while Google change API for level 24 and later:
https://android.googlesource.com/platform/frameworks/base/+/6a6cdafaec56fcd793214678c7fcc52f0b860cfc%5E%21/core/java/android/app/ContextImpl.java
I've tested in Samsung P1010 with:
//I'm in a IntentService class
File file = this.getDir("shared_prefs", MODE_PRIVATE);
I got:
"/data/data/package.name/app_shared_prefs"
It works fine to me. I can run ffmpeg in this folder.
Look:
Context.getDir
You have to create the shared_prefs directory:
try{
String dir="/data/data/package.name/shared_prefs";
// Create one directory
boolean success = (new File(dir)).mkdirs();
if (success) {
// now copy the file
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
Also... the package of your app is package.name? Make sure you are referring to the right package.

Categories

Resources