2017-06-07 2 views
0

Ich entwerfe gerade eine Android-App, die an einem bestimmten Punkt erfordert, dass der Benutzer ein Bild entweder von der Kamera oder von der Galerie des Telefons auswählt. Danach muss ich dieses Bild in der Bildansicht der Aktivität anzeigen.ImageView zeigt kein Bild aus der Telefonkamera oder Fotogalerie an

Ich denke, dass der Codeabschnitt, der das Bild bekommt, in Ordnung ist, da ich das Foto von beiden Optionen effektiv auswählen kann. Aber nachdem kein Bild mehr angezeigt wird, verschwindet das src-Logo der Bildansicht. Ich verstehe nicht, ob ich etwas falsch mache oder etwas fehlt, da es sich um einen nicht-fatalen Fehler handelt und die App weiterhin funktioniert.

So, hier sind mein XML-Layout der Aktivität:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.LinearLayoutCompat 
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" 
style="@style/Widget.Design.CoordinatorLayout" 
android:orientation="vertical" 

tools:context="pt.unl.fct.di.www.myapplication.ReportActivity"> 

<ScrollView 
    android:id="@+id/report_form" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:id="@+id/submit_report_form" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:weightSum="1"> 

     <TextView 
      android:id="@+id/type_text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="30dp" 
      android:text="@string/select_report_type" 
      android:visibility="visible" /> 
     <Spinner 
      android:id="@+id/report_type_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:layout_marginBottom="30dp"/> 


     <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/description" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:hint="Description" 
       android:inputType="textMultiLine" 
       android:selectAllOnFocus="false" 
       android:layout_marginBottom="30dp"/> 

     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="3"> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="Location" 
       android:layout_marginBottom="30dp"/> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Map" 
       android:layout_marginBottom="30dp"/> 
     </android.support.design.widget.TextInputLayout> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:src="@android:drawable/ic_menu_camera" 
      android:adjustViewBounds="true" 
      /> 

     <Button 
      android:id="@+id/camera_button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Add Picture" /> 

     <Button 
      android:id="@+id/submit_report_button" 
      style="?android:textAppearanceSmall" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="@string/action_submit_report" 
      android:textStyle="bold" 
      android:layout_weight="0.68" /> 


    </LinearLayout> 
</ScrollView> 

und die Java-Aktivitäten (wichtige Sachen):

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_report); 
    Spinner spinner = (Spinner) findViewById(R.id.report_type_spinner); 
// Create an ArrayAdapter using the string array and a default spinner layout 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.report_types, android.R.layout.simple_spinner_item); 
// Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
// Apply the adapter to the spinner 
    spinner.setAdapter(adapter); 
    mReportType = spinner.getItemAtPosition(0).toString(); 
    spinner.setOnItemSelectedListener(this); 
    mDescription = (EditText)findViewById(R.id.description); 
    mImageLocationView =(ImageView) findViewById(R.id.imageView); 
    Button cameraButton = (Button)findViewById(R.id.camera_button); 
    cameraButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
      builder.setTitle(R.string.choose_photo_from) 
        .setItems(R.array.picture_types, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // The 'which' argument contains the index position 
          // of the selected item 
          switch(which){ 
           case 0: //fotografia 
            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(takePicture, 0); 
            onPause(); 
            //zero can be replaced with any action code 
            break; 
           case 1: 
            //galeria 
            Intent pickPhoto = new Intent(Intent.ACTION_PICK, 
              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

            startActivityForResult(pickPhoto , 1); 
            //one can be replaced with any action code 
            break; 
          } 


         } 
        }); 
      builder.create().show(); 
     } 
    }); 


} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    mImageLocationView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    switch(requestCode) { 
     case 0: 
      if(resultCode == RESULT_OK){ 
       Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data"); 
       mImageLocationView.setImageBitmap(photo); 

      } 

      break; 
     case 1: 
      if(resultCode == RESULT_OK){ 
       Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data"); 
       mImageLocationView.setImageBitmap(photo); 
      } 
      break; 
    } 

} 

muß ich sagen, das ist mein allererster Android-Projekt, so bin ich irgendwie überwältigt von so viel Informationen und APIS, und auch viel experimentieren.

+0

'ACTION_PICK' verwendet kein' "data" 'extra. Der 'Uri', den Sie zurückbekommen (' imageReturnedIntent.getData() ') zeigt auf das Bild. Verwenden Sie eine Bildladebibliothek (Glide, Picasso usw.), um sie in Ihre ImageView einzufügen. – CommonsWare

Antwort

0

einfach können Sie den folgenden Code in `

if(resultCode == RESULT_OK){ 
    // the new code 
    Uri imageUri = data.getData(); 
      String[] filePath = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null); 
      cursor.moveToFirst(); 
      String path = cursor.getString(cursor.getColumnIndex(filePath[0])); 
      cursor.close(); 

      //refresh Image view 
      updateImageView(path); 
     } 
// the update image method 
    public void updateImageView(String completePath) { 
     Picasso picasoo = Picasso.with(getActivity()); 
      String path = "file://" + completePath; 
      Long tsLong = System.currentTimeMillis(); 
     String ts = tsLong.toString(); 
      picasoo.load(path+"?time="+ts).fit().centerCrop().networkPolicy(OFFLINE).into(imgUserProfile); 

}` Picasso ist ein Image-Management-Bibliothek hinzufügen, die Ihnen das Leben leicht mit verzögertes Laden das Bild.

+0

Es hat funktioniert! Und jetzt weiß ich, wie das geht, und werde Picassos API weiter erforschen. Ich danke dir sehr! – NGSBNC

+0

Sie sind herzlich willkommen, bitte geben Sie der Antwort eine Stimme. – yeaaqbi