我正在开发一个具有更多图像(约3000个)的应用程序,该文件名中的空间如下:
我是一个image.jpg
对于Android来说是新手,我现在才发现名称中包含空格或大写字母的资源不接受它,这是一个很大的问题!
我在iOS应用程序中使用了相同的图像(没有问题),现在几乎不可能更改所有名称。
有没有一种方法可以解决此问题而无需重命名图像?
如果没有,是否可以快速重命名3000张图像?
完全谢谢
您可以将它们放在资产文件夹中,并在其中使用它们,但是您将不得不将每个图像作为位图获取,而不能使用R.drawable.image_name表示法。
我使用这种方法:
public static Bitmap getFoodImage(Context context, String foodType){
InputStream bitmap = null;
Bitmap bit = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
try {
bitmap = context.getAssets().open(foodType.toUpperCase() + ".png");
if(bitmap != null){
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int sWidth = metrics.widthPixels;
options.inJustDecodeBounds = true;
Bitmap dBit = BitmapFactory.decodeStream(bitmap, null, options);
int intrinsicHeight = options.outHeight;
int intrinsicWidth = options.outWidth;
//int height = (int) (intrinsicWidth * proportion);
int nHeight = (int) (intrinsicHeight/1.3);
try {
bitmap.reset();
} catch (IOException e) {
Log.e("SGNAM", e.getMessage());
}
options.inJustDecodeBounds = false;
dBit = BitmapFactory.decodeStream(bitmap, null, options);
Bitmap bP = Bitmap.createBitmap(dBit, 0, 0, intrinsicWidth, nHeight);
if(sWidth < intrinsicWidth){
bit = Bitmap.createScaledBitmap(bP, intrinsicWidth, intrinsicHeight, false);
}
else{
bit = Bitmap.createScaledBitmap(bP, sWidth, intrinsicHeight, false);
}
bitmap.close();
dBit = null;
bP = null;
}
} catch (IOException e) {
e.printStackTrace();
}
return bit;
}
如您所见,我的所有图像名称均为大写,但是如果不需要,您可以跳过该部分(与png相同)。
如果您位于onCreate方法中,并且所需的图像称为An image.png(请记住,在此示例中,.png是由该方法添加的):
Context context = this;
Bitmap myBitmap = getFoodImage(context, An image);
您可以通过以下方式将此位图设置为imageView的资源:
ImageView myImageView = findViewById(R.id.imageView);
myImageView.setImageBitmap(myBitmap);
这应该是他的把戏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句