2013-12-13 23 views
1

不工作的相機意圖的活動結果我想從活動結果使用相機獲取圖像。它在android 4.2上正常工作。但我無法從android 2.36獲取圖像。關於在Android 2.36

任何人都可以幫我嗎?源代碼

部分連接如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      // TODO Auto-generated method stub 
      super.onActivityResult(requestCode, resultCode, data); 
      try { 
       if (requestCode == 2) {// image from camera 
        photo = (Bitmap) data.getExtras().get("data"); 

        selectedImageUri = data.getData(); 
        photo = decodeUri(selectedImageUri); 
        test.setImageBitmap(photo); 
} 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(
        getContentResolver().openInputStream(selectedImage), null, o); 

      final int REQUIRED_SIZE = 100; 

      int width_tmp = o.outWidth, height_tmp = o.outHeight; 
      int scale = 1; 
      while (true) { 
       if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
        break; 
       } 
       width_tmp /= 2; 
       height_tmp /= 2; 
       scale *= 2; 
      } 

      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeStream(
        getContentResolver().openInputStream(selectedImage), null, o2); 
     } 

回答

0

使用此:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      if (requestCode == 2) {// image from camera 
       if (resultCode == Activity.RESULT_OK) { 
      getContentResolver().notifyChange(mUri, null); 
      ContentResolver cr = getContentResolver(); 
      try { 
       mPhoto = android.provider.MediaStore.Images.Media 
         .getBitmap(cr, mUri); 

       Bitmap useThisBitmap = Bitmap.createScaledBitmap(mPhoto, 
         mPhoto.getWidth(), mPhoto.getHeight(), true); 
       test.setImageBitmap(useThisBitmap); 

      } catch (Exception e) { 
       Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } else { 
      Toast.makeText(this, 
        resultCode + "" + "=" + Activity.RESULT_CANCELED, 
        Toast.LENGTH_SHORT).show(); 
     } 

      } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
0

它看起來對我來說,這條線是不是需要:

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