2013-02-07 39 views
0

我试图从相机捕获图像并将结果放入网格视图。我使用片段如下。我收到空指针异常的OnActivityResult尝试接收意图演员:当捕获图像和接收片段内的结果时为空位图Android

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; 
     } 

回答

0

你照片保存在PATH_TO_SAVE位置。

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

File file = new File(PATH_TO_SAVE); 

Bitmap bmp = BitmapFactory.decodeFile(file.getPath()); 
+0

非常感谢。我没有注意到这一点。有效 – user2021617

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(); 
      } 


     }