daimajia / AndroidImageSlider

An amazing and convenient Android image slider.
MIT License
5.63k stars 1.66k forks source link

How to add Images chosen from gallery #305

Open RahulSabhaya opened 7 years ago

RahulSabhaya commented 7 years ago

i want to add images that chosen from gallery please suggest me...

thanhthein commented 6 years ago

Here is my custem Item Slider. Hope you can find some ideas

  BaseSliderView baseSliderView = new BaseSliderView(getActivity()) {
            @Override
            public View getView() {
                View v = LayoutInflater.from(this.getContext()).inflate(R.layout.render_type_text, null);
                ImageView target = v.findViewById(R.id.daimajia_slider_image);
                LinearLayout frame = v.findViewById(R.id.description_layout);
                frame.setBackgroundColor(Color.TRANSPARENT);
                this.bindEventAndShow(v, target);
                return v;
            }
        };
        baseSliderView
                .image("youFileHere")
                .setScaleType(BaseSliderView.ScaleType.CenterCrop)
                .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(BaseSliderView view) {
// Action here if you want
                    }
                });
        slider.addSlider(baseSliderView);
zeylevruntime commented 5 years ago

you must get your file path like this File fileImage = new File("/storage/emulated/0/Pictures/cropped2007392235.jpg HashMap<String,File> url_maps = new HashMap<String, File>(); url_maps.put("Big Bang Theory", fileImage);

davelong90 commented 5 years ago

you must get your file path like this File fileImage = new File("/storage/emulated/0/Pictures/cropped2007392235.jpg HashMap<String,File> url_maps = new HashMap<String, File>(); url_maps.put("Big Bang Theory", fileImage);

I see you pasted this answer on many questions. Could you give an example where the user selected a file from a file chooser and how you can get an absolute path from the URI returned in onActivityResult?

davelong90 commented 5 years ago

In my case I was selecting a file from a file chooser and then pushing it to a server. I first had issues just getting the Slider to show the image I selected from the chooser, but I had an even harder time getting the file to upload to the server in the correct orientation. I figured I would include my logic for the file chooser problem and correcting the orientation. What I ended up doing was creating an InputStream from the URI returned from the file chooser. I then wrote it to a temp file where I checked the orientation using ExifInterface. Based on the orientation I would rotate the image and pass that temp file path to the DefaultSliderView. Again this corrected the image orientation before pushing to the server. This way this logic didn't need to be performed fetching thousands of images in the future. Hope this helps!

            mImageUri = data.getData();
            File tempOrientedImageFile = getOrientedImageFile();
            addSlider(tempOrientedImageFile);
/**
     * Sometimes the file selected on the phone is rotated, and needs to be adjusted before uploaded
     * @return Correctly oriented image file
     */
    private File getOrientedImageFile() {
        ContentResolver cr = this.getContentResolver();
        InputStream istr;
        File tempOrientedImageFile = null;
        try {
            istr = cr.openInputStream(mImageUri);
            File tempImageFile = streamToFile(istr);
            int orientation = this.getOrientation(tempImageFile.getPath());
            Bitmap bmRotated = rotateBitmap(BitmapFactory.decodeFile(tempImageFile.getPath()), orientation);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmRotated.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte[] bitmapdata = bos.toByteArray();
            ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
            tempOrientedImageFile = streamToFile(bs);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tempOrientedImageFile;
    }
/**
     * Gets the orientation angle of the image
     * @param path Path to the image file. In this case it is a temp file location
     * @return orientation angle
     */
    private int getOrientation(String path) {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        return orientation;
    }
/**
     * Rotates the image to a normal orientation
     * @param bitmap image to flip
     * @param orientation current orientation of the image
     * @return Normal oriented bitmap image
     */
    public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
     private void addSlider(File image){
             // initialize a SliderLayout
             DefaultSliderView defaultSliderView = new DefaultSliderView(this);
             defaultSliderView
                     .image(image)
                     .setScaleType(BaseSliderView.ScaleType.Fit)
                     .setOnSliderClickListener(this);

             //add your extra information
             defaultSliderView.bundle(new Bundle());

             mDemoSlider.addSlider(defaultSliderView);}