我正在开发一个使用许多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:线程以未捕获的异常退出(组= 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):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
E / AndroidRuntime(25252):在
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):在
java.lang.reflect.Constructor.constructNative(本机方法)08-21
15:00:18.370:E / AndroidRuntime(25252):在
java.lang.reflect.Constructor.newInstance(Constructor.java:417)08-21
15:00:18.370:E / AndroidRuntime(25252):在
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):在
android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.content.res.Resources.loadDrawable(Resources.java:2988)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.content.res.TypedArray.getDrawable(TypedArray.java:602)08-21
15:00:18.370:E / AndroidRuntime(25252):在
android.widget.ImageView。 (ImageView.java:131)
08-21 15:00:18.370:E / AndroidRuntime(25252):在
android.widget.ImageView。(ImageView.java:121)
最佳答案
尝试使用Universal Image Loader或Picasso,这些库将帮助您解决OutOfmemory运行时异常,请按照以下步骤操作(使用Picasso):
1-从此处(http://square.github.io/picasso/)下载毕加索jar文件。
2-将jar复制到项目的lib文件夹中。
3-现在,右键单击您的jar文件并将其添加到构建路径。
4-现在在您的Java src文件中-在findviewById(R.id.iv12)行之后,删除所有与位图相关的代码(例如,删除所有这些-mCurrentBitmap,BitmapFactory.Options mBitmapOptions,iv1.setImageBitmap(mCurrentBitmap);)
还要从您的xml文件中删除所有src(例如android:src =“ @ drawable / kumana8”)。
5-而是使用:Picasso.with(context).load(R.drawable.kumana8).into(imageView1);
用于以编程方式将可绘制对象设置到您的imageview。
6-还要尝试在项目的android清单文件中添加android:largeHeapSize =“ true”。
您也可以尝试从此处(https://github.com/nostra13/Android-Universal-Image-Loader)或其他图像加载库尝试通用图像加载器。