2017-04-14 10 views
-1

Ich habe eine App erstellt, in der Sie die Standardkamera verwenden, um ein Foto aufzunehmen, und es dann in der Bildansicht anzeigt. Problem ist, dass das Bild nicht in der Bildansicht angezeigt wird. Versucht viele Wege, aber keine Lösung.ImageView zeigt kein Bild in Aktivität an

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

gut zwei bestehen unterschiedliche ID für Aktivität und Bildergebnis – Remario

Antwort

1

Problem ist hier:

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

Sie beginnen, die Aktivität mit requestCode ACTIVITY_START_CAMERA_APP

startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP); 

so müssen Sie wie die if-Bedingung ändern:

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

Nun, es wird nicht wissen Sie, weil die ID Sie für die Erfassung und Aktivität Ergebnis geführt unterscheidet, startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP); von requestCode == REQUEST_IMAGE_CAPTURE sie Spiel Ofto.

1

Debug innerhalb dieses: Ist es nicht vielleicht fügen Sie diese, wenn:

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

durch die Art und Weise einig Gerät nicht Bild innerhalb Daten geben. Also musst du den Weg vor dem Bild nehmen. Hier ist Beispiel:

zunächst diese innerhalb Klasse hinzufügen:

private Uri uriFilePath; 

dann fügen Sie diese irgendwo in Ihrer Klasse:

Sie haben diese zu speichern, weil die Kamera die neue Absicht

@Override 
    protected void onSaveInstanceState(Bundle outState) { 

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

fügen Sie diese einCreate:

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


     } 

hier ist Capture-Funktion:

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

    } 

} 

dann diese für innen onActivityResult get Bild verwenden:

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


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


      return; 
     } 
+0

Geck all dieser Code nicht notwendig ist, er nur verlegt die id das ist alles wirklich – Remario

+0

ich weiß aber, in Samsung-Geräte, Bild nicht innerhalb der Daten. Ich weiß das, weil ich es getestet habe viele Geräte und LG, HTC usw. geben aber Samsung nicht. Dies ist die Lösung. –

+0

wirklich welches Modell? – Remario