0

我正在尝试从相机捕获图像并将结果放入网格视图中。我使用片段如下。我在尝试接收 Intent Extras 时收到 OnActivityResult 上的空指针异常:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {       

                       Bitmap bitmap = (Bitmap) data.getExtras().get("data");


                       try {

                           FileOutputStream outStream = new FileOutputStream(PATH_TO_SAVE+"img2.png");
//NULL pointer exception here
                           bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
                           /* 100 to keep full quality of the image */

                           outStream.flush();
                           outStream.close();
                           success = true;

                           if (success) {
                               Log.i("SF", "Image saved with success");

                           } else {
                                       Log.i("SF", "Image saved with F");
                           }

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

        }

// 这里开始活动

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.submit_feed_scr2 , container, false);


        Button b = (Button) view.findViewById(R.id.btnCapture);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new


                File(PATH_TO_SAVE)));


                    startActivityForResult(intent, CAPTURE_ITEM);

                }
            });


            //Adapter
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                    items);
            GridView gridview = (GridView) view.findViewById(R.id.gridView1);

            gridview.setAdapter(adapter);

            //Providing Grid with Images

            gridview = (GridView) view.findViewById(R.id.gridView1);
            gridview.setAdapter(new ImageAdapter(view.getContext(),1));

            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    //Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });


            return view;
        }
4

2 回答 2

0

您的照片保存在 PATH_TO_SAVE 位置。

你应该在 onActivityResult 做这样的事情:

File file = new File(PATH_TO_SAVE);

Bitmap bmp = BitmapFactory.decodeFile(file.getPath());
于 2013-02-07T14:34:41.773 回答
0

试试这个,它对我有用:

if (resultCode == RESULT_OK) {

            String realPath = Constants.TMP_CAMERA_PATH;
            photoTaked = (Bitmap) data.getExtras().get("data");
            photoTakedFile = new File(realPath);
            photoTakedFile.deleteOnExit();

            if (photoTakedFile.exists()) {
                photoTakedFile.delete();
            }

            try {

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                photoTaked.compress(Bitmap.CompressFormat.PNG, 90, bytes);

                photoTakedFile.createNewFile();
                FileOutputStream fo = new FileOutputStream(photoTakedFile);
                fo.write(bytes.toByteArray());
                fo.close();

                uploadImageView.setImageBitmap(photoTaked);
                uploadImageView.setVisibility(View.VISIBLE);

                calcRemainingChars();

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


        }
于 2013-02-07T14:32:32.643 回答