take picture from camera and choose from gallery and display in Image view

后端 未结 3 1387
醉梦人生
醉梦人生 2020-12-06 08:43

I want to take photos from gallery and set it in my ImageView as well as I want to take photo from camera and set it into my same Image View.

I am very much stuck on

3条回答
  •  一个人的身影
    2020-12-06 08:52

    You must implement this code to take image from camera or gallery :

    Take this variable : 
    
    AlertDialog dialog;
    private static final int IMAGE_PICK = 1;
    private static final int IMAGE_CAPTURE = 2;
    private Bitmap profile_imageBitmap;
    

    On Button click event :

    if (v == btn_uploadPhoto) {
        dialog.show();
        }
    

    Then in your activity's oncreate method :

    final String[] items = new String[] { "Take from camera",
                "Select from gallery" };
        ArrayAdapter adapter = new ArrayAdapter(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 item) {
    
                if (item == 0) {
                    path = "";
                    Intent intent = new Intent(
                            "android.media.action.IMAGE_CAPTURE");
                    File folder = new File(Environment
                            .getExternalStorageDirectory() + "/LoadImg");
    
                    if (!folder.exists()) {
                        folder.mkdir();
                    }
                    final Calendar c = Calendar.getInstance();
                    String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                            + ((c.get(Calendar.MONTH)) + 1) + "-"
                            + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
                            + "-" + c.get(Calendar.MINUTE) + "-"
                            + c.get(Calendar.SECOND);
    
                    path = String.format(
                            Environment.getExternalStorageDirectory()
                                    + "/LoadImg/%s.png", "LoadImg(" + new_Date
                                    + ")");
                    File photo = new File(path);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photo));
                    startActivityForResult(intent, 2);
    
                } else { // pick from file
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Choose a Photo"),
                            IMAGE_PICK);
                }
            }
        });
    
        dialog = builder.create();
    

    Now Out of Oncreate :

        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK
                || requestCode == IMAGE_CAPTURE) {
            switch (requestCode) {
            case IMAGE_PICK:
                this.imageFromGallery(resultCode, data);
    
                img_myProfile.setImageBitmap(null);
    
                img_myProfile.setImageBitmap(setphoto);
    
                break;
    
            case IMAGE_CAPTURE:
    
                this.imageFromGallery(resultCode, data);
    
                img_myProfile.setImageBitmap(null);
    
                img_myProfile.setImageBitmap(setphoto);
    
                break;
            default:
                break;
            }
        }
    }
    
    private void imageFromGallery(int resultCode, Intent data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
    
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    
        profile_Path = cursor.getString(columnIndex);
        cursor.close();
    
        setphoto = BitmapFactory.decodeFile(profile_Path);
    
    }
    
    private void imageFromCamera(int resultCode, Intent data) {
        updateImageView((Bitmap) data.getExtras().get("data"));
    }
    
    private void updateImageView(Bitmap newImage) {
        setphoto = newImage.copy(Bitmap.Config.ARGB_8888, true);
    }
    
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

提交回复
热议问题