Android Bitmap 内存溢出解决办法

//解决内存溢出情况
	private void SetImageSize(String path,ImageView imageView){
		BitmapFactory.Options opt = new BitmapFactory.Options(); 
        //这个isjustdecodebounds很重要                
        opt.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeFile(path, opt);
         
        //获取到这个图片的原始宽度和高度
         int picWidth  = opt.outWidth;
         int picHeight = opt.outHeight;
         
        //获取屏的宽度和高度
         WindowManager windowManager = ((Activity) context).getWindowManager();
         Display display = windowManager.getDefaultDisplay();
         int screenWidth = display.getWidth();
         int screenHeight = display.getHeight();
         
         //isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2
         opt.inSampleSize = 1;
         //根据屏的大小和图片大小计算出缩放比例
         if(picWidth > picHeight){
                 if(picWidth > screenWidth)
                         opt.inSampleSize = picWidth/screenWidth;
         }
         else{
                 if(picHeight > screenHeight)

                         opt.inSampleSize = picHeight/screenHeight;
         }
         
         //这次再真正地生成一个有像素的,经过缩放了的bitmap
         opt.inJustDecodeBounds = false;
         bm = BitmapFactory.decodeFile(path, opt);
         
         //用imageview显示出bitmap
         imageView.setImageBitmap(bm);
	}

 移动开发网:www.chengdu135.com

展开阅读全文

android内存溢出!!求具体的解决办法

12-22

package micr3.login;rnrnimport java.io.IOException;rnrnimport android.app.Activity;rnimport android.content.Context;rnimport android.content.ContextWrapper;rnimport android.content.Intent;rnimport android.content.SharedPreferences;rnimport android.content.SharedPreferences.Editor;rnimport android.graphics.Bitmap;rnimport android.graphics.BitmapFactory;rnimport android.graphics.BitmapFactory.Options;rnimport android.net.Uri;rnimport android.os.Bundle;rnimport android.os.ParcelFileDescriptor;rnimport android.view.View;rnimport android.view.View.OnClickListener;rnimport android.widget.Button;rnimport android.widget.CheckBox;rnimport android.widget.EditText;rnimport android.widget.Toast;rnrnpublic class Logining1Activity extends Activity rn /** Called when the activity is first created. */rn EditText username;rn EditText password;rn CheckBox p1,p2,p3,p4;rn Button logining;rn String name;rn String pasd;rn @Overridern public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn username=(EditText)findViewById(R.id.autocomplete);rn password=(EditText)findViewById(R.id.password);rn p1=(CheckBox)findViewById(R.id.plain_cb1);rn p2=(CheckBox)findViewById(R.id.plain_cb2);rn p3=(CheckBox)findViewById(R.id.plain_cb3);rn p4=(CheckBox)findViewById(R.id.plain_cb4);rn logining=(Button)findViewById(R.id.logining);rn rn p1.setOnClickListener(new OnClickListener() rn rn public void onClick(View v) rn // TODO Auto-generated method stubrn name = username.getText().toString();rn pasd = password.getText().toString();rn SharedPreferences sp = getSharedPreferences("preferences",rnrn Context.MODE_PRIVATE);rn Editor editor = sp.edit();rnrn editor.putString("name", name);rnrn editor.putString("password", pasd);rn if (editor.commit())rnrn Toast.makeText(Logining1Activity.this,rnrn R.string.save_success, 1).show();rnrn elsern Toast.makeText(Logining1Activity.this,rn R.string.save_failed, 1).show();rn rn );rn logining.setOnClickListener(new OnClickListener() rn rn public void onClick(View v) rn // TODO Auto-generated method stubrn rnrnrn String name1 = username.getText().toString();rn String pasd1 = password.getText().toString();rn if(name1=="qq"||pasd1=="qq")rn Intent intent=new Intent();rn intent.setClass(Logining1Activity.this, Mainface.class);rn startActivity(intent);rn Logining1Activity.this.finish();rn rn rn elsern Toast.makeText(Logining1Activity.this,rn R.string.wrong, 1).show();rn rn rnrn rn rn rn );rn rn rn rn rn rn rn在如上的Activity中我该怎样使用Bitmap方法,或者还有更好的办法 求各位帮忙,同时标明注释 谢谢!!!! 论坛

bitmap 内存溢出 decodeByteArray

11-09

public classMainActivity extends Activity rn private static final String TAGrnrn = "MainActivity";rn private SurfaceView surfaceView;rn private Camera camera;rnrn @Overridern public void onCreate(Bundlernrn savedInstanceState) rn super.onCreate(savedInstanceState);rnrn Window window = getWindow();rn requestWindowFeaturernrn (Window.FEATURE_NO_TITLE);// 没有标题rn window.setFlagsrnrn (WindowManager.LayoutParams.FLAG_FULLSCREEN,rn WindowManager.LayoutParams.rnrn FLAG_FULLSCREEN);// 设置全屏rn window.addFlagsrnrn (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 高亮rnrn setContentView(R.layout.main);rnrn surfaceView =rnrn (SurfaceView) findViewById(R.id.surfaceView);rnrn surfaceView.getHolder().setFixedSize(176, 144); // 设置分辨率rn /*rn *rn * 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用rn *rn * 户面前rn */rn surfaceView.getHolder().setTypernrn (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);rnrn surfaceView.getHolder().addCallback(new SurfaceCallback());rn rnrn private final class SurfaceCallback implementsrnrn SurfaceHolder.Callback rn private boolean preview;rnrn @Overridern public void surfaceChangedrnrn (SurfaceHolder holder, int format, int width,rnrn int height) rn rnrn @Overridern publicrnrnvoid surfaceCreated(SurfaceHolder holder) rn //打开摄像头rn camera = Camera.open();rn rnrnWindowManager wm = (WindowManager) getSystemServicernrn(Context.WINDOW_SERVICE);rn Display display =rnrnwm.getDefaultDisplay();rn Camera.Parametersrnrnparameters = camera.getParameters();rn rnrnparameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小rn parameters.setPreviewFrameRaternrn(3);//每秒3帧rn parameters.setPictureFormatrnrn(PixelFormat.JPEG);//设置照片的输出格式rn rnrnparameters.set("jpeg-quality", 85);//照片质量rn rnrnparameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小rn camera.setParameters(parameters);rn rnrn try rn rnrncamera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面rn camera.startPreview();rn rnrn preview = true;rn catchrnrn(IOException e) rn Log.e(TAG, e.toStringrnrn());rn rn rnrn @Overridern public void surfaceDestroyed(SurfaceHolder holder) rnrn if (camera != null) rn if (preview) rnrn camera.stopPreview();rnrn rn camera.release();rnrn rn rnrn rnrn @Overridern public boolean onKeyDown(int keyCode, KeyEvent event) rn ifrnrn (camera != null && event.getRepeatCount() == 0) rn switchrnrn (keyCode) rn case KeyEvent.KEYCODE_SEARCH:// 搜索键rnrn // 自动对焦rnrn camera.autoFocus(null);rn return true;rnrn case KeyEvent.KEYCODE_CAMERA:// 拍照键rnrn case KeyEvent.KEYCODE_DPAD_CENTER:// 确定键rnrn camera.takePicture(null, null, new TakePictureCallback());rnrn return true;rn rn rnrn return super.onKeyDown(keyCode, event);rn rnrn private final class TakePictureCallback implements PictureCallback rnrn @Overridern public void onPictureTaken(byte[] data,rnrn Camera camera) rn try rnrn Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,rn data.length);rnrn File file = new Filernrn (Environment.getExternalStorageDirectory(), Systemrn .currentTimeMillis()rn +rnrn ".jpg");rn FileOutputStream outStream =rnrn new FileOutputStream(file);rnrn bitmap.compress(CompressFormat.JPEG, 100, outStream);rnrn outStream.close();rnrn camera.stopPreview();rn camera.startPreview();rnrn catch (Exception e) rnrn Log.e(TAG, e.toString());rn rn rnrn rnrnrnrnrnrnrnrnrnrn rn- rn rn 论坛

没有更多推荐了,返回首页