这是我的android片段代码,它有一个图像。 我的问题是当我选择一个在某些Android版本中有图像的片段时,它强制关闭。 请帮助我解决问题。
爪哇
import com.imagezoom.ImageAttacher;
import com.imagezoom.ImageAttacher.OnMatrixChangedListener;
import com.imagezoom.ImageAttacher.OnPhotoTapListener;
import info.paarva.CrescentBuilders.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
public class FragmentTab2 extends Fragment {
ImageView mImaView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_tulip_locationmap, container, false);
mImaView = (ImageView) rootView.findViewById(R.id.locationmap_tulip);
Bitmap bimtBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.tulip_locationmap);
mImaView.setImageBitmap(bimtBitmap);
/**
* Use Simple ImageView
*/
usingSimpleImage(mImaView);
return rootView;
}
public void usingSimpleImage(ImageView imageView) {
ImageAttacher mAttacher = new ImageAttacher(imageView);
ImageAttacher.MAX_ZOOM = 2.0f; // Double the current Size
ImageAttacher.MIN_ZOOM = 1.0f; // Half the current Size
MatrixChangeListener mMaListener = new MatrixChangeListener();
mAttacher.setOnMatrixChangeListener(mMaListener);
PhotoTapListener mPhotoTap = new PhotoTapListener();
mAttacher.setOnPhotoTapListener(mPhotoTap);
}
private class PhotoTapListener implements OnPhotoTapListener {
@Override
public void onPhotoTap(View view, float x, float y) {
}
}
private class MatrixChangeListener implements OnMatrixChangedListener {
@Override
public void onMatrixChanged(RectF rect) {
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".locationmap_tulip"
android:background="@color/textclr_actnbar"
>
<ImageView
android:id="@+id/locationmap_tulip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/tulip_locationmap" />
</RelativeLayout>
logcat的
12-13 06:54:11.792: W/dalvikvm(1417): threadid=1: thread exiting with uncaught exception (group=0xa4ca7b20)
12-13 06:54:11.800: E/AndroidRuntime(1417): FATAL EXCEPTION: main
12-13 06:54:11.800: E/AndroidRuntime(1417): Process: info.paarva.CrescentBuilders, PID: 1417
12-13 06:54:11.800: E/AndroidRuntime(1417): java.lang.OutOfMemoryError
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:452)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:482)
12-13 06:54:11.800: E/AndroidRuntime(1417): at Tulip.FragmentTab2.onCreateView(FragmentTab2.java:29)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.Fragment.performCreateView(Fragment.java:1700)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.BackStackRecord.run(BackStackRecord.java:684)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.os.Handler.handleCallback(Handler.java:733)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.os.Handler.dispatchMessage(Handler.java:95)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.os.Looper.loop(Looper.java:136)
12-13 06:54:11.800: E/AndroidRuntime(1417): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-13 06:54:11.800: E/AndroidRuntime(1417): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 06:54:11.800: E/AndroidRuntime(1417): at java.lang.reflect.Method.invoke(Method.java:515)
12-13 06:54:11.800: E/AndroidRuntime(1417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-13 06:54:11.800: E/AndroidRuntime(1417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-13 06:54:11.800: E/AndroidRuntime(1417): at dalvik.system.NativeStart.main(Native Method)
12-13 06:59:11.828: I/Process(1417): Sending signal. PID: 1417 SIG: 9
12-13 06:59:12.196: D/dalvikvm(1468): GC_FOR_ALLOC freed 93K, 4% free 3765K/3920K, paused 6ms, total 7ms
12-13 06:59:12.212: D/dalvikvm(1468): GC_FOR_ALLOC freed 2K, 4% free 3983K/4144K, paused 12ms, total 12ms
12-13 06:59:12.228: I/dalvikvm-heap(1468): Grow heap (frag case) to 6.366MB for 2536932-byte allocation
答案 0 :(得分:1)
正如你所见,它正在关闭,因为它的内存不足。如果您控制此图像的分辨率,请不要使其大于现在最高分辨率的手机(2,560 x 1,440)。但是考虑到较低分辨率的设备,绝大多数设备仍然在the Android docs for handling large bitmaps中实施建议。他们的基本技术是首先检查位图尺寸,然后根据设备屏幕尺寸进行缩放,以便只有可显示的像素保存在内存中。