BitmapFactory.decodeStream returns null(jar 文件中的图像)
BitmapFactory.decodeStream returns null (Image in jar file)
我正在尝试将图像从 Unity3d 交换到 java。
我从 Unity 发送:
var filePath = System.IO.Path.Combine(Application.streamingAssetsPath, scene.thumbnail.name + ".jpg");
此 returns Android 中的以下路径(通过 log.i 看到):
jar:file:///data/app/com.package.name/base.apk!/assets/image_1.jpg
我正在尝试将此图像解码为位图:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/assets/image_1.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null)
imageItems.add(new ImageItem(bitmap, "Image#" + i));
但是,位图始终为空。我错过了什么?我指的是错误的路径吗?我如何告诉 java 解码统一提供的路径中的文件?
谢谢!!
我想通了:)
我必须使用 AssetManager:
AssetManager assetManager = getAssets();
InputStream stream;
try {
stream = assetManager.open(imagePathList.get(i));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null)
imageItems.add(new ImageItem(bitmap, "Image#" + i));
} catch (IOException ex) {
return null;
}
我正在尝试将图像从 Unity3d 交换到 java。
我从 Unity 发送:
var filePath = System.IO.Path.Combine(Application.streamingAssetsPath, scene.thumbnail.name + ".jpg");
此 returns Android 中的以下路径(通过 log.i 看到):
jar:file:///data/app/com.package.name/base.apk!/assets/image_1.jpg
我正在尝试将此图像解码为位图:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/assets/image_1.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null)
imageItems.add(new ImageItem(bitmap, "Image#" + i));
但是,位图始终为空。我错过了什么?我指的是错误的路径吗?我如何告诉 java 解码统一提供的路径中的文件? 谢谢!!
我想通了:)
我必须使用 AssetManager:
AssetManager assetManager = getAssets();
InputStream stream;
try {
stream = assetManager.open(imagePathList.get(i));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null)
imageItems.add(new ImageItem(bitmap, "Image#" + i));
} catch (IOException ex) {
return null;
}