选择布局中的图像

时间:2013-05-16 22:44:38

标签: android eclipse

我正在尝试使用单选按钮在我的应用程序中选择一个图像,但是当我在手机上运行该应用程序时它不起作用,我不知道错误在哪里。

我有一个主函数(它在主布局上有四个按钮)调用SelectImage:

 Button button1 = (Button)this.findViewById(R.id.Button2);
    botonempezar.setOnClickListener(new OnClickListener(){

        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this, SelectImage.class);
            startActivity(intent);
           }
        });

然后,当你预先按下那个按钮时,它应该在SelectImage类中打开下一个布局(但它没有那个)。这是该类的代码:

public class SelectImage extends MainActivity {

protected static final int SELECT_PICTURE = 0;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectimage);

        Button bAccept = (Button)findViewById(R.id.buttonaccept);
        bAccept.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
            RadioButton b1 = (RadioButton)findViewById(R.id.select);
            int code=0;
            Intent intent = null;

            if (b1.isChecked()) {
                intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                code = SELECT_PICTURE;
            }
            startActivityForResult(intent, code);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImage = data.getData();
    InputStream is;
    try {
        is = getContentResolver().openInputStream(selectedImage);
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bitmap1 = BitmapFactory.decodeStream(bis);
        Bitmap bitmap = Bitmap.createScaledBitmap(bitmap1, 640, 480, true); 
        ImageView iv = (ImageView)findViewById(R.id.imageView);
        iv.setImageBitmap(bitmap);

        }catch (FileNotFoundException e) {} 
            catch (IOException e) {
                e.printStackTrace();
            }
    }
}

希望有人能找到我的错误。谢谢!

0 个答案:

没有答案