0
Ich habe den Code für die Auswahl eines Bildes von SD oder Kamera implementiert. Aber mein Bild erscheint nicht in ImageView
und ich kann kein Bild von der SD-Karte wählen.Android Wählen Sie Bild oder Kamera
Wenn ich auf ein Bild von einer SD-Karte klicke, springt es sofort zurück zu meiner Aktivität.
final String[] items = new String[] {"From Camera","From SD Card"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter(adapter,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
if(which == 0){
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"event_image"+String.valueOf(System.currentTimeMillis()+".jpg"));
imageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageCaptureUri);
intent.putExtra("return data",true);
startActivityForResult(intent,PICK_FROM_CAMERA);
}catch(Exception ex){
ex.printStackTrace();
}
dialog.cancel();
}else{
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Complete action using"),PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = builder.create();
mImageView = (ImageView) findViewById(R.id.imageView);
imagePicker = (ImageButton) findViewById(R.id.image_picker);
imagePicker.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
dialog.show();
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode != RESULT_OK)
return;
Bitmap bitmap = null;
String path ="";
if(requestCode == PICK_FROM_FILE) {
imageCaptureUri = data.getData();
path = getRealPathFromURI(imageCaptureUri);
if(path == null)
path = imageCaptureUri.getPath();
if(path != null){
bitmap = BitmapFactory.decodeFile(path);
}
}else{
path = imageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
mImageView.setImageBitmap(bitmap);
}
public String getRealPathFromURI(Uri contentURI){
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentURI,proj,null,null,null);
if(cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Mögliches Duplikat [Dialogbild aus der Galerie zu holen oder von der Kamera] (http://stackoverflow.com/questions/10165302/dialog-to- pick-image-from-gallery-or-from-camera) –
Verwenden Sie einfach diese Bibliothek: https://github.com/jrvansuita/PickImage –