2017-04-14 47 views
-1

我做了一個應用程序,您使用默認相機拍攝一張照片,然後將其顯示在圖像視圖上。問題是,圖像不顯示在圖像視圖中。嘗試了很多方法,但沒有解決方案。ImageView在活動中沒有顯示圖像

mainactivity.java:

public class MainActivity extends Activity { 

    private static final int ACTIVITY_START_CAMERA_APP = 0; 
    static final int REQUEST_IMAGE_CAPTURE = 1; 
    private ImageView mPhotoCapturedImageView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mPhotoCapturedImageView = (ImageView) findViewById(R.id.capturePhotoImageView); 
    } 

    public void takePhoto(View view){ 
     Intent callCameraApplicationIntent = new Intent(); 
     callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Toast.makeText(this, "Picture taken sucessfully!", Toast.LENGTH_SHORT).show(); 
     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      mPhotoCapturedImageView.setImageBitmap(imageBitmap); 
     } 
    } 
} 

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.taufiq.ocrdemo.MainActivity"> 

    <ImageView 
     android:id="@+id/capturePhotoImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/photoButton" 
     android:layout_marginBottom="19dp" 
     android:layout_marginEnd="43dp" 
     android:layout_marginLeft="43dp" 
     android:layout_marginRight="43dp" 
     android:layout_marginStart="43dp" 
     android:layout_marginTop="16dp" 
     android:contentDescription="@string/preview" 
     android:minHeight="300dp" 
     app:layout_constraintBottom_toTopOf="@+id/photoButton" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:ignore="MissingConstraints" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" 
     tools:minHeight="300dp" /> 

    <Button 
     android:id="@+id/photoButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="takePhoto" 
     android:text="@string/capture_photo" 
     android:layout_weight="1" 
     tools:ignore="MissingConstraints" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintBottom_creator="1" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintLeft_creator="1" 
     android:layout_marginBottom="27dp" 
     app:layout_constraintLeft_toLeftOf="parent" /> 

</android.support.constraint.ConstraintLayout> 
+0

以及你通過兩個不同的ID爲活動和圖像結果 – Remario

回答

1

問題是在這裏:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 

您從開始requestCode活動ACTIVITY_START_CAMERA_APP

startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP); 

,所以你需要修改的,如果條件一樣:

if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) { 
1

那麼它不會你知道的,因爲你通過捕獲和活動結果的ID是不同的,從startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);他們requestCode == REQUEST_IMAGE_CAPTURE比賽OFTO。

1

這裏面調試:也許它不是插入此,如果:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      mPhotoCapturedImageView.setImageBitmap(imageBitmap); 
     } 

的方式有些設備不給內部的數據圖像。所以你必須在拍照之前給路徑。下面是示例:因爲相機是新的意圖

@Override 
    protected void onSaveInstanceState(Bundle outState) { 

     if (uriFilePath != null) outState.putString("uri_file_path", uriFilePath.toString()); 
     super.onSaveInstanceState(outState); 
    } 

你要存儲此:

首先添加此內部類:

private Uri uriFilePath; 

然後加上你的類裏面這個地方

補充說明創建:

if (savedInstanceState != null) { 
      if (uriFilePath == null && savedInstanceState.getString("uri_file_path") != null) { 
       uriFilePath = Uri.parse(savedInstanceState.getString("uri_file_path")); 
      } 


     } 

這裏是拍攝功能:

private void captureImage() { 
    PackageManager packageManager = this.getPackageManager(); 
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
     File mainDirectory = new File(Environment.getExternalStorageDirectory(), "Tofaş");//tmp 
     if (!mainDirectory.exists()) 
      mainDirectory.mkdirs(); 

     Calendar calendar = Calendar.getInstance(); 
     cameraFileName = "IMG_" + calendar.getTimeInMillis()+".jpg"; 
     uriFilePath = Uri.fromFile(new File(mainDirectory, cameraFileName)); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, uriFilePath); 
     startActivityForResult(intent, RC_CAMERA); 

    } 

} 

然後裏面onActivityResult用這個獲取圖像:

if (requestCode == RC_CAMERA && resultCode == RESULT_OK) { 


     uriFilePath.getPath()// is the path of your image 


      return; 
     } 
+0

花花公子所有的代碼是不必要的,他只是錯誤的編號,這是真的 – Remario

+0

我知道,但在三星設備,圖像不在數據。我知道這是因爲我測試了很多設備和lg,htc等,但三星不是。這是解決方案。 –

+0

真的是哪個型號? – Remario