2017-04-14 63 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