package micr3.login;import java.io.IOException;import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;public class Logining1Activity extends Activity {
    /** Called when the activity is first created. */
EditText username;
EditText password;
CheckBox p1,p2,p3,p4;
Button logining;
String name;
String pasd;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         username=(EditText)findViewById(R.id.autocomplete);
          password=(EditText)findViewById(R.id.password);
          p1=(CheckBox)findViewById(R.id.plain_cb1);
          p2=(CheckBox)findViewById(R.id.plain_cb2);
          p3=(CheckBox)findViewById(R.id.plain_cb3);
          p4=(CheckBox)findViewById(R.id.plain_cb4);
          logining=(Button)findViewById(R.id.logining);
    
          p1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
  name = username.getText().toString();
                   pasd = password.getText().toString();
                  SharedPreferences sp = getSharedPreferences("preferences",                          Context.MODE_PRIVATE);
                  Editor editor = sp.edit();                  editor.putString("name", name);                  editor.putString("password", pasd);
                  if (editor.commit())                      Toast.makeText(Logining1Activity.this,                                        R.string.save_success, 1).show();            else
                      Toast.makeText(Logining1Activity.this,
                            R.string.save_failed, 1).show();
}
});
          logining.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

String name1 = username.getText().toString();
String pasd1 = password.getText().toString();
               if(name1=="qq"||pasd1=="qq"){
                Intent intent=new Intent();
                intent.setClass(Logining1Activity.this, Mainface.class);
                startActivity(intent);
               Logining1Activity.this.finish();
                
               }
               else{
                Toast.makeText(Logining1Activity.this,
                           R.string.wrong, 1).show();
              
               }

}
});
          
 
          
          
    }
    
}在如上的Activity中我该怎样使用Bitmap方法,或者还有更好的办法 求各位帮忙,同时标明注释 谢谢!!!!

解决方案 »

  1.   

    Bitmap的使用是尽量使用小图,尽量做到不使用的时候就回收
    这里有篇文章介绍关于避免OOM错误
    http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html
      

  2.   

    ● 主要是加上这段: 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
                    options.inSampleSize = 2; ● eg1:(通过Uri取图片) 
    private ImageView preview; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
                        options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一 
                        Bitmap bitmap = BitmapFactory.decodeStream(cr 
                                .openInputStream(uri), null, options); 
                        preview.setImageBitmap(bitmap); 
    以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。 
    ● eg2:(通过路径去图片) 
    private ImageView preview; 
    private String fileName= "/sdcard/DCIM/Camera/2010-05-14 16.01.44.jpg"; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
                    options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一 
                            Bitmap b = BitmapFactory.decodeFile(fileName, options); 
                            preview.setImageBitmap(b); 
                            filePath.setText(fileName); 
    http://ming-fanglin.iteye.com/blog/1132564
      

  3.   

    package micr3.login;import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;
    import dalvik.system.VMRuntime;public class Logining1Activity extends Activity {
        /** Called when the activity is first created. */
    EditText username;
    EditText password;
    CheckBox p1,p2,p3,p4;
    Button logining;
    String name;
    String pasd; private final static float TARGET_HEAP_UTILIZATION = 0.75f;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 
             username=(EditText)findViewById(R.id.autocomplete);
              password=(EditText)findViewById(R.id.password);
              p1=(CheckBox)findViewById(R.id.plain_cb1);
              p2=(CheckBox)findViewById(R.id.plain_cb2);
              p3=(CheckBox)findViewById(R.id.plain_cb3);
              p4=(CheckBox)findViewById(R.id.plain_cb4);
              logining=(Button)findViewById(R.id.logining);
        
              p1.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    // TODO Auto-generated method stub
      name = username.getText().toString();
                       pasd = password.getText().toString();
                      SharedPreferences sp = getSharedPreferences("preferences",                          Context.MODE_PRIVATE);
                      Editor editor = sp.edit();                  editor.putString("name", name);                  editor.putString("password", pasd);
                      if (editor.commit())                      Toast.makeText(Logining1Activity.this,                                        R.string.save_success, 1).show();            else
                          Toast.makeText(Logining1Activity.this,
                                R.string.save_failed, 1).show();
    }
    });
              logining.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    // TODO Auto-generated method stub

    String name1 = username.getText().toString();
    String pasd1 = password.getText().toString();
                   if(name1=="qq"||pasd1=="qq"){
                    Intent intent=new Intent();
                    intent.setClass(Logining1Activity.this, Mainface.class);
                    startActivity(intent);
                   Logining1Activity.this.finish();
                    
                   }
                   else{
                    Toast.makeText(Logining1Activity.this,
                               R.string.wrong, 1).show();
                  
                   }

    }
    });
              
              
              
              
              
              
              
              
              
              
              
              
              
        }
        
    }对不??
      

  4.   

     VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 
    能解决问题吗,
    求结果
      

  5.   

    在你认为图片资源已经作废后的情况下,请调用强制资源回收。
    system.gc()
    当然,这意味着你要很清楚你的资利用情况。在这在我的开发项目中有效的解决了问题。
      

  6.   

    system.gc()是收回全部的,还是什么