2017-04-20 3 views
-1

Ich weiß, es gibt viele Beiträge wie diese, aber meins ist ein wenig Besonderes.Android - ImageView/ImageButton mit Bild von Galerie oder Kamera

Problem Beschreibung:

ich eine öffentliche Klasse wollen, die einen kleinen Dialog zeigt Bildquelle, Kamera oder eine Galerie auszuwählen. Nach der Auswahl zeigt ein ImageView das Bild an und das Bild wird im App-Ordner gespeichert. Diese Klasse wird von vielen Aktivitäten aufgerufen. Also, ich habe diesen Code:

PhotoActivity.java

package com.app.test; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.provider.MediaStore; 

import java.util.ArrayList; 
import java.util.List; 


public class PhotoActivity { 

    private Context context; 

    public PhotoActivity(Context current){ 
     this.context = current; 
    } 
    //this will be called from other activities 
    public void Change_Photo(final Activity a) { 

     List<String> options = new ArrayList<String>(); 
     options.add(context.getResources().getString(R.string.camera)); 
     options.add(context.getResources().getString(R.string.gallery)); 
     options.add(context.getResources().getString(R.string.cancel)); 

     //Create sequence of items 
     final CharSequence[] Sources = options.toArray(new String[options.size()]); 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); 
     dialogBuilder.setTitle(context.getResources().getString(R.string.select_image_source)); 
     dialogBuilder.setItems(Sources, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       if (item==0) {//camera 
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        a.startActivityForResult(takePicture, 0);//zero can be replaced with any action code 
       }else if (item == 1) {//gallery 
        Intent pickPhoto = new Intent(Intent.ACTION_PICK, 
          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        a.startActivityForResult(pickPhoto , 1);//one can be replaced with any action code 
       } 
      } 
     }); 
     //Create alert dialog object via builder 
     AlertDialog alertDialogObject = dialogBuilder.create(); 
     //Show the dialog 
     alertDialogObject.show(); 
    } 
} 

Ich habe in der Manifest-Datei hinzufügen:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.CAMERA" /> 

Andere Aktivitäten haben einen Image oder Image wie folgt aus:

<ImageButton 
     android:id="@+id/btn_picture" 
     android:layout_width="140dp" 
     android:layout_height="140dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="false" 
     android:elevation="0dp" 
     android:gravity="center_horizontal" 
     android:src="@drawable/camera" /> 

, die ein Kamera-Bild am Anfang hat, wird aber s wie das ausgewählte Bild am Ende.

Was muss als Nächstes getan werden, um das ausgewählte oder aufgenommene Foto in diesem ImageButton anzuzeigen?

Antwort

0
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
Uri uri = data.getData(); 
try { 
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
// imageButton where you want to set image 
imageButton.setImageBitmap(bitmap); 
} catch(IOException e) { 
e.printStackTrace(); 
} 
} 

So können Sie Ihr ausgewähltes/aufgenommenes Bild auf ImageButton einstellen.

Hoffe, das wird helfen.

+1

Eine bessere Option als 'getBitmap()' und 'setImageBitmap()' wäre die Verwendung einer [Bildlade-Bibliothek] (https://android-arsenal.com/tag/46) wie Picasso. Der in der Antwort gezeigte Code lädt das Bild im Hauptanwendungs-Thread, wodurch die Benutzeroberfläche eingefroren wird. – CommonsWare

+0

Das hat den Job gemacht. Eine kleine Frage. Wenn ich ein neues Foto anwende, zeigt ImageButton es um 90 Grad nach links gedreht an. Warum? –

+0

Dies hängt von der Größe des Fotos ab, nehmen Sie ein kleines Bild und versuchen Sie es erneut –

Verwandte Themen