2016-04-27 14 views
2

Ich habe einige Beispiele auf SO gefolgt, wie man ein Bild von Camera oder Gallary retrive. Der Kamerateil funktioniert, aber der Gallertenteil nicht. Der Code scheint für mich sehr diffus zu sein, also weiß ich nicht, worauf genau ich achten soll.Auswahl eines Bildes aus der Kamera funktioniert, aber nicht aus dem Gallary [VIDEO DEMO]

Ich habe auch die erforderlichen Berechtigungen in meinem Manifest. Hier

ist ein Video des Problems: https://www.youtube.com/watch?v=OOoY1y4W86w

ImagePicker (Ansicht V), Absicht/Choosers, Dateien, URIS

public void ImagePicker(View v) { 

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 
     if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { 

      final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary" + File.separator); 
      rootdir.mkdirs(); 
      final String filename = "img_" + System.currentTimeMillis() + ".jpg"; 
      final File sdImageMainDirecotry = new File(rootdir, filename); 
      outputFileUri = Uri.fromFile(sdImageMainDirecotry); 
      Log.d("TAG", "IM HERE 1"); 

      //camera 
      final List<Intent> cameraIntents = new ArrayList<>(); 
      final Intent CameraCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      final PackageManager packageManager = getPackageManager(); 
      final List<ResolveInfo> listcam = packageManager.queryIntentActivities(CameraCaptureIntent, 0); 
      Log.d("TAG", "IM HERE 2"); 


      for (ResolveInfo res : listcam) { 

       final String packageName = res.activityInfo.packageName; 
       final Intent intent = new Intent(CameraCaptureIntent); 
       intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
       intent.setPackage(packageName); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
       cameraIntents.add(intent); 
       Log.d("TAG", "IM HERE 3"); 
      } 


      //Gallary 
      final Intent imageChooser = new Intent(); 
      imageChooser.setType("image/*"); 
      imageChooser.setAction(Intent.ACTION_GET_CONTENT); 


      // Chooser of filesystem options. 
      final Intent chooserIntent = Intent.createChooser(imageChooser, "Select Source"); 

      // Add the camera options. 

      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); 
      startActivityForResult(chooserIntent, SELECT_FROM_GALLARY); 


     } else { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 
     } 
    } else { 
     Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show(); 
    } 
} 

onActivityResult():

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_FROM_GALLARY) { 
      final boolean isCamera; 
      if (data == null) { 
       isCamera = true; 
      } else { 
       final String action = data.getAction(); 
       if (action == null) { 
        isCamera = false; 
       } else { 
        isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE); 
       } 
      } 

      Uri selectedImageUri; 
      if (isCamera) { 

       selectedImageUri = outputFileUri; 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 8; 
       final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath(), options); 
       Drawable drawable = new BitmapDrawable(getResources(), bitmap); 
       pic.setBackground(drawable); 

      } else { 
       selectedImageUri = data == null ? null : data.getData(); 
       Log.d("ImageURI", selectedImageUri.getLastPathSegment()); 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 8; 
       try { 

        InputStream input = getContentResolver().openInputStream(selectedImageUri); 
        final Bitmap bitmap = BitmapFactory.decodeStream(input, null, options); 

        Drawable drawable = new BitmapDrawable(getResources(), bitmap); 
        pic.setBackground(drawable); 


       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

Antwort

0

Verwenden Sie den folgenden Code, um ein Bild aus der Galerie auszuwählen.

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    //Here PICK_FROM_GALLERY is a requestCode 
    startActivityForResult(intent, PICK_FROM_GALLERY); 

In onActivityResult():

if (resultCode == Activity.RESULT_OK && requestCode == PICK_FROM_GALLERY) { 
     if (data.getData() != null) { 
      mImageUri = data.getData(); 
     } else { 
      //showing toast when unable to capture the image 
      Debug.toastValid(context, "Unable to upload Image Please Try again ..."); 
     } 
} 
+0

Diese arbeiten nicht mit dem Code, den ich habe. Hast du meinen Code gesehen? – Muddz

+0

Wohin gibst du die Absicht für die Galerie ...? Bitte c meine Antwort, dort bin ich Aktion zu einer Absicht übergeben. –

+0

Ich habe es hier: 'final Intent imageChooser = new Intent(); imageChooser.setType ("image/*"); imageChooser.setAction (Intent.ACTION_GET_CONTENT); ' und dann mache ich' Intent chooserIntent = Intent.createChooser (imageChooser, "Quelle auswählen"); ' – Muddz

Verwandte Themen