2015-09-17 38 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); 
}