2015-09-17 43 views
1

我是Android新手。我正在测试一个Android应用程序(在互联网上搜索后创建),该应用程序在主要活动中创建一个Intent按钮,启动设备的摄像头并将拍摄的照片返回到主布局中的ImageView。 我的问题是,虽然在模拟器上测试过程成功完成(在保存照片后图片显示在ImageView上),但在测试三星平板电脑2(GT-P7510)与Android时发生了同样的情况版本4.0.4,但是当我试图运行在三星S4智能手机(GT-I9500)与版本5.0.1它保存图片,但照片是未显示ImageView在主要布局。onActivityResult完成后照片未显示在主要活动上

有人有同样的问题吗?

我读过它可能是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); 
     }  
} 
+0

你不需要这个showPhoto(String uri)方法。 您可以使用ImageView.setImageURI方法,该方法使用从成功意图获取的URI。 这里是链接http://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri) –

+0

谢谢尼古拉。我已经尝试过,但也没有工作。 – Cloio

+0

我发现的是,由于S4摄像头拍摄的照片数量庞大,这就是ImageView上没有显示的原因。我发现下一个链接的解决方案http://stackoverflow.com/questions/29932060/imageview-will-not-load-via-setimageuri-when-ran-on-samsung-galaxy-s4-only(谢谢AJ9! )为我工作。唯一的问题是现在图像旋转90度,并且如果选择肖像始终显示在横向。 – Cloio

回答

0

这是图像旋转的解决方案。 旋转将是现在和将不同从设备到设备... 所以代码:

private void fixImage(String filepath) throws IOException { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap b = BitmapFactory.decodeFile(filepath, options); 

    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; 
    } 
    saveImageToFile(rotateImage(b, angle), filepath); 

} 

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

谢谢尼古拉。不幸的是,这种解决方案不起作用(我已经尝试过)。无论如何,我将你的代码添加到我的代码中,现在是最糟糕的,保存图片后S4 Screen Layout上没有任何内容出现。 – Cloio

+0

这是代码: – Cloio

+0

private void showPhoto(String photoUri)throws IOException { \t \t File imageFile = new File(photoUri); \t \t如果(imageFile.exists()){ \t \t \t photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER); \t \t \t String filepath = imageFile.getAbsolutePath(); \t \t \t BitmapFactory.Options options = new BitmapFactory。选项(); \t \t \t options.inPreferredConfig = Bitmap.Config.ARGB_8888; \t \t \t位图B = BitmapFactory.decodeFile(文件路径,选项); \t \t \t ExifInterface EI =新ExifInterface(文件路径); \t \t \t INT取向= ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL); – Cloio

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