我有以下代码从res文件夹中检索bmp文件。我将所有图像存储在drawable-hdpi文件夹中,并且它不适用于HTC One V(primoc)。任何帮助将不胜感激。
以下是代码:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
这是logcat
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1105)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:554)
at scahagamecenter.com.LeadersListView.decodeSampledBitmapFromResource(LeadersListView.java:128)
at scahagamecenter.com.LeadersListView.getView(LeadersListView.java:110)
at android.widget.AbsListView.obtainView(AbsListView.java:2075)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1476)
at android.widget.ListView.onMeasure(ListView.java:1339)
at android.view.View.measure(View.java:12948)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
at android.view.View.measure(View.java:12948)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12948)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2145)
at android.view.View.measure(View.java:12948)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1227)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)