我想写一段代码把sd卡中图片读取入Imageview中,并将图片的信息保存在EditText中,供后续使用。代码如下
public class second extends Activity{
/** Called when the activity is first created. */
private ImageButton btn1;//打开文件
private EditText edt3;//显示第二幅图片位置
private ImageButton btn3;//Embedded
public ImageButton btn4;//extract
private ImageButton btn5;//save
private EditText edt;//password
private EditText edt1;//message
private ImageView img1;
public ProgressBar bar; 
public int p=0;//进度条提示
    //public Image fpic = new Image();//也许错
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = getIntent();
        findview();
        btn1.setOnClickListener(new MyButtonListener());//打开文件,寻找图片
        setImg();
        String location = intent.getStringExtra("location");
        BitmapFactory.Options options = new BitmapFactory.Options();    
        options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一     
   Bitmap image =  BitmapFactory.decodeFile(location, options);//get first image
       image =  image.copy(Bitmap.Config.ARGB_8888, true);//Guarantees that the image is decoded in the ARGB8888 format 
       int imlen= 0;
       imlen = (int) image.getHeight()*image.getWidth();
       int[] pixel= new int[imlen];
       image.getPixels(pixel,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());//get first image's pixels*/
}
public void setImg() {
// TODO Auto-generated method stub
Bundle b=this.getIntent().getExtras();
     if(b!=null){
     String str;
     str =b.getString("FileName"); 
     edt3.setText(str);
     Bitmap map=BitmapFactory.decodeFile(b.getString("FileName"));
     img1.setImageBitmap(map);
     }
}
public void findview(){
     btn1 = (ImageButton) findViewById(R.id.btn1);
     edt3 = (EditText) findViewById(R.id.edt3);
     btn3 = (ImageButton) findViewById(R.id.btn3);
     btn4 = (ImageButton) findViewById(R.id.btn4);
     btn5 = (ImageButton) findViewById(R.id.btn5);
     edt = (EditText) findViewById(R.id.edt0);
     edt1 =(EditText) findViewById(R.id.edt1);
     img1 = (ImageView) findViewById(R.id.img1);
     bar = (ProgressBar)findViewById(R.id.firstBar);
    }
class MyButtonListener implements OnClickListener {   
     @Override
     public void onClick (View v)
     {
     //TODO Auto-generated method stub
     Intent in=new Intent();
in.setClass(second.this, FileMang.class);
startActivity(in);
           // b=1;//定义点击顺序的变量
            finish();
     }
    }
}
如果没有标红的地方,可以运行成功,但有了那段代码就报如下错误
05-24 03:24:18.469: ERROR/AndroidRuntime(493): Uncaught handler: thread main exiting due to uncaught exception
05-24 03:24:18.519: ERROR/AndroidRuntime(493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tmxk/com.tmxk.second}: java.lang.NullPointerException
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.os.Looper.loop(Looper.java:123)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at java.lang.reflect.Method.invoke(Method.java:521)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at dalvik.system.NativeStart.main(Native Method)
05-24 03:24:18.519: ERROR/AndroidRuntime(493): Caused by: java.lang.NullPointerException
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at com.tmxk.second.onCreate(second.java:41)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-24 03:24:18.519: ERROR/AndroidRuntime(493):     ... 11 more
请教大侠帮助啊

解决方案 »

  1.   

    空指针啊!image 是null吗?
      

  2.   

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    if(options != null && location != null)
    {
    options.inSampleSize = 2; 
    Bitmap image = BitmapFactory.decodeFile(location, options);//get first image
    if(image != null)
    {
    image = image.copy(Bitmap.Config.ARGB_8888, true);
    int imlen= 0;
    imlen = (int) image.getHeight()*image.getWidth();
    int[] pixel= new int[imlen];
    image.getPixels(pixel,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());
    }
    }
      

  3.   

    大侠,我后面的程序还要用到pixel数组,等到我再用时,它双给我一个空指针的报错,是怎么回事啊?我后面的类要用pixel数组该怎么改啊。。