2016-04-27 13 views
2

カメラやギャラリーから画像を取得する方法のいくつかの例があります。カメラパーツは機能しますが、ギャラリーパーツは表示されません。コードは私のために理解するのが非常に難解だと思われるので、正確に何を見ているのかわかりません。カメラから画像を選んだりギャラリーから来なかった[ビデオデモ]

私のマニフェストにも必要な権限があります。ここで

は、問題のビデオです:https://www.youtube.com/watch?v=OOoY1y4W86w

ImagePicker(ビューV)、意図/チュー、ファイル、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(); 
       } 
      } 
     } 
    } 
} 

答えて

0

以下のコードを使用してギャラリーから画像を選択します。 onActivityResult()で

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); 

:私が持っているコードで

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

このいけない仕事。私のコードを見たことがありますか? – Muddz

+0

ギャラリーのインテントアクションをどこに渡していますか?私の答えをお聞かせください。 –

+0

私はそれをここに持っています: 'final Intent imageChooser = new Intent(); imageChooser.setType( "image/*"); imageChooser.setAction(Intent.ACTION_GET_CONTENT); ' そして次に私は' Intent chooserIntent = Intent.createChooser(imageChooser、 "Select Source"); ' – Muddz