csh_34 2013-01-28 09:19 采纳率: 0%
浏览 2641
已采纳

当从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 2013-01-29 02: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);
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题