onActivityResult完成后,主要活动上的照片未显示

时间:2015-09-17 21:54:59

标签: android imageview samsung-mobile

我是Android新手。我正在测试一个Android应用程序(在互联网上搜索后构建),它在主要活动中创建一个Intent,点击按钮启动,启动设备的Camera并将照片返回到主布局中的ImageView。 我遇到的问题是,在模拟器上测试过程成功完成(保存照片后图片显示在ImageView上),在三星平板电脑2 上进行测试时也会出现相同情况(GT-P7510) )使用Android 版本4.0.4 ,但当我尝试在三星S4智能手机(GT-I9500)5.0.1版上运行时,它会保存图片,但是照片在主布局上未在ImageView中显示

是否有人遇到同样的问题?

我读过它可能是OnCreate上的一些问题,但无法解决。

这是代码的一部分。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_camera);

    photoImage = (ImageView) findViewById(R.id.photo_image);

    callCameraButton = (Button) 
    findViewById(R.id.button_callcamera);

    photoImage.setVisibility(View.VISIBLE);

    callCameraButton.setOnClickListener( new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = Uri.fromFile(getOutputPhotoFile());
            i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            archivo = fileUri.getEncodedPath();
            startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
        if (resultCode == RESULT_OK) {
          Uri photoUri = null;
          if (data == null) {
            // A known bug here! The image should have saved in fileUri
            Toast.makeText(this, "Image saved successfully", 
                           Toast.LENGTH_LONG).show();
            photoUri = fileUri;
          } else {
            photoUri = data.getData();
            Toast.makeText(this, "Image saved successfully in: " + data.getData(), 
                           Toast.LENGTH_LONG).show();
          }
          showPhoto(archivo);

        } else if (resultCode == RESULT_CANCELED) {
          Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(this, "Callout for image capture failed!", 
                         Toast.LENGTH_LONG).show();
        }
    }
}

private void showPhoto(String photoUri) {
      File imageFile = new File (photoUri);
      if (imageFile.exists()){
         Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
         BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
         photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
         photoImage.setImageDrawable(drawable);
      }       
}

2 个答案:

答案 0 :(得分:0)

这是图像旋转的解决方案。 该旋转将存在并且将随设备而不同...... 所以代码:

"Class 'reddit' not found"

答案 1 :(得分:0)

最后,我是如何在您的帮助和其他搜索中解决问题的。

private void showPhoto(String photoUri) throws IOException {
      File imageFile = new File (photoUri);
      if (imageFile.exists()){
            photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
            String filepath = imageFile.getAbsolutePath();
            Bitmap b = createScaledBitmap(filepath, photoImage.getWidth(), photoImage.getHeight());
            ExifInterface ei = new ExifInterface(filepath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            float angle = 0;
            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
                default:
                    angle = 0;
            }
            photoImage.setImageBitmap(RotateBitmap(b, angle));
      }
}

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}