2 csh 34 csh_34 于 2013.01.28 17:19 提问

当从gallery中加载图像时,有空指针异常

我用下面的代码从 Gallery中获取图片。给出一个空指针异常并且系统崩溃。我在设备上测试了代码,只要我在gallery中选择一个图像时,系统就崩溃了。哪些地方出错了呢?

AlertDialog.Builder builder = new AlertDialog.Builder(CreatePod.this);
                        builder.setMessage("Select") .setCancelable(false).setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id)                            {
                                   Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
                                    gallIntent.setType("image/*"); 
                                    startActivityForResult(gallIntent, 10);
                               }
                        })

protected  void onActivityResult(int requestCode, int resultCode, Intent data){
     super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 10:
            if (resultCode == Activity.RESULT_OK) {
                 Bundle extras = data.getExtras();
                   Bitmap b = (Bitmap) extras.get("data");
                   imgView.setImageBitmap(b);

                String timestamp = Long.toString(System.currentTimeMillis());
                   MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
                HttpResponse httpResponse;
                ByteArrayOutputStream bao = new ByteArrayOutputStream();

            b.compress(Bitmap.CompressFormat.JPEG, 100, bao);

            byte [] ba = bao.toByteArray();
            int f = 0;
            String ba1=Base64.encodeToString(ba, f);

2个回答

curie_87
curie_87   2013.01.29 10:01
已采纳

把下面的代码

Bitmap b = (Bitmap) extras.get("data");   
imgView.setImageBitmap(b); 

改成

Uri imageUri = data.getData(); 
Bitmap b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imgView.setImageBitmap(b);

我运行过这段代码,是好用的。

clcurie_11
clcurie_11   2013.02.01 10:04

你的错误是照片不能在 extras 中返回,它的 Uri 是在 getData()中.

   switch (requestCode) {
        case 10:
            if (resultCode == Activity.RESULT_OK) {
                Uri bitmapUri = data.getData();

                try {

                    Bitmap b = Media.getBitmap(getContentResolver(), bitmapUri);
                    mImgView.setImageBitmap(b);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // ... etc ...
                break;
            }
    }

在 AlertDialogBuilder 中

public void myClickHandler(View clickTarget) {
    Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
    gallIntent.setType("image/*");
    gallIntent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(gallIntent, 10);
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!