Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NinePatchDrawable from Drawable.createFromPath或FileInputStream_Android_Drawable_Nine Patch - Fatal编程技术网

Android NinePatchDrawable from Drawable.createFromPath或FileInputStream

Android NinePatchDrawable from Drawable.createFromPath或FileInputStream,android,drawable,nine-patch,Android,Drawable,Nine Patch,当从drawable.createFromPath或使用InputStream的许多其他方法加载时,我无法使九个补丁drawable工作(即,它正在拉伸九个补丁的图像) 从when资源加载时,加载相同的九个补丁可以正常工作。以下是我正在尝试的方法: Button b = (Button) findViewById(R.id.Button01); Drawable d = null; //From Resources (WORKS!) d = getResources().getDrawabl

当从drawable.createFromPath或使用InputStream的许多其他方法加载时,我无法使九个补丁drawable工作(即,它正在拉伸九个补丁的图像)

从when资源加载时,加载相同的九个补丁可以正常工作。以下是我正在尝试的方法:

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);

我有一个非常类似的问题。我正试图从通过网络检索的
位图
创建一个
NinePatchDrawable
。我还没有找到解决那个问题的办法。由于
Drawable.createFrom…
方法显然不起作用,我唯一的选择似乎是了解如何格式化九块补丁,并调用
NinePatch
构造函数。有关详细信息,请参阅。

我回答了一个非常类似的关于非基于资源的ninepatches的问题。要从.apk归档文件中读取文件,我使用以下方法:

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