2016-08-09 47 views
-2

Auf Knopfdruck kann der Benutzer zwischen der Aufnahme eines Bildes oder der Auswahl eines Bildes viermal wählen. Diese vier URIs werden in einer ArrayList gespeichert und die vier sollten in ihren jeweiligen ImageViews angezeigt werden. Das Problem, das ich habe, ist, dass wenn ich ein Bild "aufnehme", es nicht sofort in ImageView angezeigt wird; aber die Bilder, die ich aus der Galerie gewählt habe, tun es.Bild wird bei der Auswahl aus der Galerie angezeigt, aber nicht bei der Aufnahme von der Kamera

Das von mir aufgenommene Bild ist richtig gespeichert und kann beim nächsten Drücken der Taste in "Choose from gallery" gefunden werden. Kann jemand sehen, was ich falsch mache?

takePictureIntent():

private void dispatchTakePictureIntent() { 
    for(int i = 0; i < 4; i++) { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
       outputFileUri = Uri.fromFile(photoFile); 
      } catch (IOException ex) { 
       Log.w("error","IOException"); 
      }catch (NullPointerException nullEx) { 
       Log.w("error","NullPointerException"); 
      } 
      // Camera. 
      final List<Intent> cameraIntents = new ArrayList<Intent>(); 
      final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      final PackageManager packageManager = getPackageManager(); 
      final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 
      for (ResolveInfo res : listCam) { 
       final String packageName = res.activityInfo.packageName; 
       final Intent intent = new Intent(captureIntent); 
       intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
       intent.setPackage(packageName); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
       cameraIntents.add(intent); 
      } 
      // Filesystem. 
      final Intent galleryIntent = new Intent(); 
      galleryIntent.setType("image/*"); 
      galleryIntent.setAction(Intent.ACTION_OPEN_DOCUMENT); 
      // Chooser of filesystem options. 
      final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); 
      // Add the camera options. 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); 
      if(id.equals(HAPPY_ID)) 
       startActivityForResult(chooserIntent, REQUEST_HAPPY_PHOTO); 
      if(id.equals(SURPRISED_ID)) 
       startActivityForResult(chooserIntent, REQUEST_SURPRISED_PHOTO); 
      if(id.equals(AFRAID_ID)) 
       startActivityForResult(chooserIntent, REQUEST_AFRAID_PHOTO); 
      if(id.equals(UPSET_ID)) 
       startActivityForResult(chooserIntent, REQUEST_UPSET_PHOTO); 
      if(id.equals(SAD_ID)) 
       startActivityForResult(chooserIntent, REQUEST_SAD_PHOTO); 
     } 
    } 
} 

onActivityResult():

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == REQUEST_HAPPY_PHOTO || requestCode == REQUEST_SURPRISED_PHOTO || requestCode == REQUEST_AFRAID_PHOTO || 
        requestCode == REQUEST_UPSET_PHOTO || requestCode == REQUEST_SAD_PHOTO) { 
       final boolean isCamera; 
       if (data == null) { 
        isCamera = true; 
       } else { 
        final String action = data.getAction(); 
        if (action == null) { 
         isCamera = false; 
        } else { 
         isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        } 
       } 
       Uri selectedImageUri; 
       if (isCamera) { 
        selectedImageUri = outputFileUri; 
       } else { 
        selectedImageUri = data == null ? null : data.getData(); 
       } 
       //Log.d("doing ids", "right before id"); 
       //Log.d("doing ids", "id is " + id); 
       if(requestCode == REQUEST_HAPPY_PHOTO) { 
        //Log.d("doing ids", "in happy"); 
        happyList.add(selectedImageUri); 
       } 
       if(requestCode == REQUEST_SURPRISED_PHOTO) { 
        //Log.d("doing ids", "in surprised"); 
        surprisedList.add(selectedImageUri); 
       } 
       if(requestCode == REQUEST_AFRAID_PHOTO) { 
        //Log.d("doing ids", "in surprised"); 
        afraidList.add(selectedImageUri); 
       } 
       if(requestCode == REQUEST_UPSET_PHOTO) { 
        //Log.d("doing ids", "in surprised"); 
        upsetList.add(selectedImageUri); 
       } 
       if(requestCode == REQUEST_SAD_PHOTO) { 
        //Log.d("doing ids", "in surprised"); 
        sadList.add(selectedImageUri); 
       } 
      } 
     } 
    } 

Antwort

0

statt 4-mal Looping, warum nicht jeder Imageview ein OnClickListener das Bild einer nach dem anderen zu bekommen?

+0

ich die Idee zu schätzen wissen! Aber das Szenario, das ich in meiner Frage skizziert habe, repräsentiert etwas, das dem entspricht, was ich tatsächlich in meiner Aktivität mache. Die Funktionalität ist im Wesentlichen die gleiche, so eine Antwort auf diese Frage wird mir mit meiner tatsächlichen Situation helfen. – rafvasq

0

dieses Versuchen, das funktioniert für mich

public Uri captureImage() { 
    Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (chooserIntent.resolveActivity(getPackageManager()) != null) { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.TITLE, "Image File name"); 
     Uri takenImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
     Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, takenImageUri); 
     startActivityForResult(intentPicture, MyConstants.REQUEST_IMAGE_CAPTURE); 
     return takenImageUri; 
    } 
    return null; 
} 


public String getRealPathFromURI(Uri contentUri){ 
    try { 
     String[] proj = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    catch (Exception e){ 
     return contentUri.getPath(); 
    } 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK && data != null) { 
     switch (requestCode){ 
      case MyConstants.REQUEST_IMAGE_CAPTURE: 
       if (takenImageUri != null) { 
        Uri imagePath =getRealPathFromURI(takenImageUri,this)); 
       } 
     } 
    } 
} 
+0

Damit kann der Benutzer zwischen der Aufnahme oder Auswahl von Bildern wählen? – rafvasq