我正在开发一个使用许多jpeg图像的应用程序。它总是给出outOfMemory错误异常。然后我尝试解码它。但它仍然给出错误。请帮我解决一下这个。我无法理解错误在哪里。 提前致谢。
public class Kumana extends FragmentActivity implements LocationListener{
GoogleMap mGoogleMap;
ArrayList<LatLng> mMarkerPoints;
double mLatitude = 0;
double mLongitude = 0;
Bitmap mCurrentBitmap=null;
BitmapFactory.Options mBitmapOptions;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper,mViewFlipper2,mViewFlipper3;
private AnimationListener mAnimationListener;
ImageView iv1, iv2, iv3,iv4,iv5,iv6,iv7,iv8,iv9,iv10,iv11,iv12;
private Context mContext;
int mCurrentIndex=0;
@SuppressWarnings("deprecation")
private final GestureDetector detector = new GestureDetector(
new SwipeGestureDetector());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kumana);
final int[] imgIDs={R.drawable.blackneckedstork,R.drawable.eurasianspoonbill,R.drawable.greaterrackettaileddrongo,
R.drawable.kumana5,R.drawable.lesseradjunt,R.drawable.lesserwhistlingduck,R.drawable.malabartrogan,R.drawable.redfacedmalkoha,
R.drawable.sirkeermalkoha,R.drawable.thegreatthickknee,R.drawable.yellowfootedgreenpigeon};
iv1=(ImageView)findViewById(R.id.imgvw_kumana1);
iv2=(ImageView)findViewById(R.id.imgvw_kumana2);
iv3=(ImageView)findViewById(R.id.imgvw_kumana3);
iv4=(ImageView)findViewById(R.id.imgvw_kumana_breeding3);
iv5=(ImageView)findViewById(R.id.imgvw_kumana_breeding1);
iv6=(ImageView)findViewById(R.id.imgvw_kumana_breeding2);
iv7=(ImageView)findViewById(R.id.imgvw_kumana_breeding4);
iv8=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants1);
iv9=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants2);
iv10=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants3);
iv11=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants4);
iv12=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants5);
mBitmapOptions = new BitmapFactory.Options();
mBitmapOptions.inJustDecodeBounds=true;
BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
mCurrentBitmap=Bitmap.createBitmap(mBitmapOptions.outWidth,mBitmapOptions.outHeight,Bitmap.Config.ARGB_8888);
mBitmapOptions.inJustDecodeBounds=false;
mBitmapOptions.inBitmap=mCurrentBitmap;
mBitmapOptions.inSampleSize=1;
BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
iv1.setImageBitmap(mCurrentBitmap);
mCurrentIndex=(mCurrentIndex+1)%imgIDs.length;
BitmapFactory.Options bitmapOptions=null;
bitmapOptions=mBitmapOptions;
bitmapOptions.inBitmap=mCurrentBitmap;
mCurrentBitmap=BitmapFactory.decodeResource(getResources(), imgIDs[mCurrentIndex],bitmapOptions);
iv1.setImageBitmap(mCurrentBitmap);
mContext = this;
mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana1);
mViewFlipper2 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana2);
mViewFlipper3 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana3);
// sets auto flipping
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(8000);
mViewFlipper.startFlipping();
mViewFlipper2.setAutoStart(true);
mViewFlipper2.setFlipInterval(8000);
mViewFlipper2.startFlipping();
mViewFlipper3.setAutoStart(true);
mViewFlipper3.setFlipInterval(8000);
mViewFlipper3.startFlipping();
mViewFlipper.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
mViewFlipper2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
mViewFlipper3.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
// animation listener
mAnimationListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// animation started event
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO animation stopped event
}
};
class SwipeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showNext();
mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
// controlling animation
mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper2.showNext();
mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
// controlling animation
mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper3.showNext();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showPrevious();
mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
// controlling animation
mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper2.showPrevious();
mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
// controlling animation
mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper3.showPrevious();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
//Google Map methods
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper_kumana1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/kumana8" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaLbl" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/kumana5" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaLbl" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/lesserwhistlingduck" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaLbl" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:id="@+id/swipe_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/right_arrow" />
<ImageView
android:id="@+id/swipe_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/right_arrow2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper_kumana2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana_breeding3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/eurasianspoonbill" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc4_bird3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana_breeding1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/blackneckedstork" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc4_bird1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana_breeding2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/lesseradjunt" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc4_bird2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumana_breeding4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/thegreatthickknee" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc4_bird4" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:id="@+id/swipe_left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/arrowleft" />
<ImageView
android:id="@+id/swipe_right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/arrowright" />
</RelativeLayout>
<TextView
android:id="@+id/kumanaDesc5_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:paddingTop="10dp"
android:text="@string/kumanaDesc5_header"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/kirinddescripion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:paddingTop="10dp"
android:text="@string/kumanaDesc6_RareMigrants"
android:textSize="15sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper_kumana3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumanaDesc6_RareMigrants1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/yellowfootedgreenpigeon" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc6_RareMigrants1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumanaDesc6_RareMigrants2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/greaterrackettaileddrongo" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc6_RareMigrants2_graterRacket" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumanaDesc6_RareMigrants3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/malabartrogan" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc6_RareMigrants3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumanaDesc6_RareMigrants4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/redfacedmalkoha" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc6_RareMigrants4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgvw_kumanaDesc6_RareMigrants5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/imageVw"
android:scaleType="centerCrop"
android:src="@drawable/sirkeermalkoha" />
<TextView
style="@style/ImageTitle"
android:text="@string/kumanaDesc6_RareMigrants4" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:id="@+id/swipe_left1_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/arrowleft" />
<ImageView
android:id="@+id/swipe_right1_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/imageVw"
android:src="@drawable/arrowright" />
</RelativeLayout>
<fragment
android:id="@+id/map_kumana"
android:layout_width="match_parent"
android:layout_height="466dp"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
</ScrollView>
threadid = 1:线程以未捕获的异常退出(group = 0x41df1700) E / AndroidRuntime(25252):致命异常:主
E / AndroidRuntime(25252):java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.isuru.allaboutkataragama / com.isuru.allaboutkataragama.Kumana}: android.view.InflateException:二进制XML文件行#165:错误 膨胀班
E / AndroidRuntime(25252):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) E / AndroidRuntime(25252):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)08-21 15:00:18.370:E / AndroidRuntime(25252):引起: java.lang.reflect.InvocationTargetException
08-21 15:00:18.370:E / AndroidRuntime(25252):at java.lang.reflect.Constructor.constructNative(Native Method)08-21 15:00:18.370:E / AndroidRuntime(25252):at java.lang.reflect.Constructor.newInstance(Constructor.java:417)08-21 15:00:18.370:E / AndroidRuntime(25252):at android.view.LayoutInflater.createView(LayoutInflater.java:600)08-21 15:00:18.370:E / AndroidRuntime(25252):... 28更多
08-21 15:00:18.370:E / AndroidRuntime(25252):引起: java.lang.OutOfMemoryError
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.content.res.Resources.loadDrawable(Resources.java:2988)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.content.res.TypedArray.getDrawable(TypedArray.java:602)08-21 15:00:18.370:E / AndroidRuntime(25252):at android.widget.ImageView。 (ImageView.java:131)
08-21 15:00:18.370:E / AndroidRuntime(25252):at android.widget.ImageView。(ImageView.java:121)
答案 0 :(得分:1)
尝试使用Universal Image Loader或Picasso,这些库将帮助您理清OutOfmemory运行时异常,请按照以下步骤操作(使用Picasso):
1-从这里下载picasso jar文件(http://square.github.io/picasso/)。
2-将jar复制到项目的lib文件夹中。
3-现在右键单击您的jar文件并将其添加到构建路径。
4-现在在您的java src文件中 - 在您的findviewById(R.id.iv12)行之后,删除所有与Bitmap相关的代码(例如,删除所有这些 - mCurrentBitmap,BitmapFactory.Options mBitmapOptions,iv1.setImageBitmap(mCurrentBitmap); ) 还要从xml文件中删除所有src(例如android:src =“@ drawable / kumana8”)。
5-而是使用:Picasso.with(context).load(R.drawable.kumana8).into(imageView1); 用于以编程方式将drawable设置为imageview。
6-同时尝试在项目的android清单文件中添加android:largeHeapSize =“true”。
此外,您可以从此处(https://github.com/nostra13/Android-Universal-Image-Loader)或其他图像加载库尝试通用图像加载器。
答案 1 :(得分:0)
您将大位图加载到您的应用中。尝试减小位图的大小或添加
android:largeHeapSize="true"
进入你的清单。
答案 2 :(得分:0)
我认为你的图片尺寸很大。你已经用1或2张图片运行了吗?如果您可以运行1或2个错误依赖于图像的图像。您需要减小图像大小。只是我的想法。希望得到帮助。 Compress PNG image tool
答案 3 :(得分:0)
答案 4 :(得分:0)
首先,您需要确定哪些尺寸应具有图像。 我的例子尺寸是1920x1080: 对于地图标记,您可以使用100x100位图。
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath);
int srcWidth = o.outWidth;
int srcHeight = o.outHeight;
int dstWidth = 1920;
int dstHeight = 1080;
int inSampleSize = 1;
if (srcHeight > dstHeight || srcWidth > dstWidth) {
final int halfHeight = srcHeight / 2;
final int halfWidth = srcWidth / 2;
while ((halfHeight / inSampleSize) > dstWidth
&& (halfWidth / inSampleSize) > dstWidth) {
inSampleSize *= 2;
}
}
o.inSampleSize = inSampleSize;
o.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o);
此外,您可以使用Bitmap.recycle()释放内存。