Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24658] Hyperloop: Android - How to instantiate native Android Drawable?

GitHub Issuen/a
TypeStory
PriorityCritical
StatusClosed
ResolutionInvalid
Resolution Date2017-09-25T18:55:09.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsHyperloop
Labelsandroid, hyperloop, hyperloop-examples
ReporterJose Zuniga
AssigneeGary Mathews
Created2017-05-04T16:24:29.000+0000
Updated2017-09-25T18:55:12.000+0000

Description

*Problem* I am trying to reference an image file in Hyperloop to instantiate a native Android object (for example, a Drawable). I am currently using Titanium Hyperloop to create a custom Android class that I wish to port over to my app built on Appcelerator. From the Hyperloop documentation, I can reference the native Android Drawable with:
Drawable = require('android.graphics.drawable.Drawable')
Now, I wish to instantiate a Drawable object through the native Drawable.createFromPath() constructor but my problem is that Titanium is not recognizing the path name to my image in my resources folder in my app directory. For example, the path to my image file is: {my_app}/assets/images/myImage.png Any paths I try to pass into the constructor give a "No such file or directory" exception. How do I work with Titanium Hyperloop to construct a native Android Drawable object? *Test Case*
var Drawable = require('android.graphics.drawable.Drawable');
var imagePath = '/images/myImage.png';
	
var mImage = Drawable.createFromPath(imagePath);
*Log* "No such file or directory" exception *Community Discussion* [http://stackoverflow.com/questions/43726570/titanium-hyperloop-how-to-instantiate-native-android-drawable]

Comments

  1. Hans Knöchel 2017-05-04

    [~gmathews] I guess the native developer needs to specify a more advanced path to that method right? Since the relative path needs to be mapped to the exact (drawable) directory. [~jzuniga] Maybe only the file name? Or check where the images are copied to in the resulting apk and check the Android docs on how to resolve to that path.
  2. Gary Mathews 2017-05-10

    You would need to use the full path, or you could use a stream:
       var Drawable = require('android.graphics.drawable.Drawable'),
           Activity = require('android.app.Activity'),
           activity = new Activity(Ti.Android.currentActivity),
           context = activity.getApplicationContext(),
           assetStream = context.getAssets().open('Resources/assets/images/a.png');
       
       if (assetStream) {
           alert('drawable: ' + Drawable.createFromStream(assetStream, null));
           assetStream.close();
       }
       

JSON Source