Tim H
Tim H

Reputation: 13543

NinePatchDrawable from Drawable.createFromPath or FileInputStream

I cannot get a nine patch drawable to work (i.e. it is stretching the nine patch image) when loading from either Drawable.createFromPath or from a number of other methods using an InputStream.

Loading the same nine patch works fine when loading from when resources. Here's the methods I am trying:

Button b = (Button) findViewById(R.id.Button01);

Drawable d = null;

//From Resources (WORKS!)
d = getResources().getDrawable(R.drawable.test);

//From Raw Resources (Doesn't Work)
InputStream is = getResources().openRawResource(R.raw.test);
d = Drawable.createFromStream(is, null);

//From Assets (Doesn't Work)
try {
InputStream is = getResources().getAssets().open("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}

//From FileInputStream (Doesn't Work)
try {
FileInputStream is = openFileInput("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}

//From File path (Doesn't Work)
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");


if (d != null) b.setBackgroundDrawable(d);

Upvotes: 0

Views: 5123

Answers (2)

kanzure
kanzure

Reputation: 1329

I answered a very similar question here about non-resource-based ninepatches. To read a file from within .apk archives, I use something like this:

String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);

Upvotes: 1

jacob11
jacob11

Reputation: 669

I have a very similar problem. I'm trying to create a NinePatchDrawable from a Bitmap that I've retrieved over the network. I still haven't found a solution to that problem. Since the Drawable.createFrom... methods apparently doesn't work, my only option seems to be to understand how to format the nine-patch chunk, and to invoke the NinePatch constructor. See the SO-thread for details.

Upvotes: 0

Related Questions