未使用通过相机捕获的图像设置ImageView

时间:2016-06-14 06:20:36

标签: javascript android camera gallery

我使用CircleImageView库使用了'de.hdodenhof:circleimageview:2.0.0'

代码:

 @Override
       public boolean onMenuItemClick(MenuItem item){

           switch(item.getItemId()){
               case R.id.slot1:
                   Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                   if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                       // Create the File where the photo should go
                       File photoFile = null;
                       try {
                           photoFile = createImageFile();
                       } catch (IOException ex) {
                           ex.printStackTrace();
                       }
                       // Continue only if the File was successfully created
                       if (photoFile != null) {
                           cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                           startActivityForResult(cameraIntent, CAMERA_IMAGE_REQUEST);
                       }
                   }
                   return true;
               case R.id.slot2:
                   Intent i = new Intent();
                   i.setType("image/*");
                   i.setAction(Intent.ACTION_GET_CONTENT);
                   startActivityForResult(Intent.createChooser(i, "Select Picture"), PICK_IMAGE_REQUEST);
                   return true;
               default:
                   return true;
           }
       }


    private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "Widget_profile_" + timeStamp + "_";
            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  // prefix
                    ".jpg",         // suffix
                    storageDir      // directory
            );

            // Save a file: path for use with ACTION_VIEW intents
            photoPath = "file:" + image.getAbsolutePath();
            return image;
        }

并在onActivityResult()方法中:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Bitmap photo = BitmapFactory.decodeFile(photoPath);
            profile = (ImageView)findViewById(R.id.profile_image);
            profile.setImageBitmap(photo);

        } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            profile = (ImageView) findViewById(R.id.profile_image);
            profile.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我创建了一个菜单选项,可以在图库相机之间进行选择,其中图库看起来效果很好,但相机选项不会设置任何图像。

public void setImage(View view){

        PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.getMenu().add(1, R.id.slot1, 1, "Camera");
        popupMenu.getMenu().add(1,R.id.slot2,2,"Gallery");
        popupMenu.show();
    }

有人可以帮帮我吗?

0 个答案:

没有答案