简体   繁体   中英

Move a picture from gallery to drawable using code. [android studio]

I am using this two functions to open the image gallery and to choose a picture. I have the uri of the picture and the picture itself in "inputStream". My question is how can I move the picture or to copy it in the folder drawable from android studio ?

public void onImageGalleryClicked(View v) {
                 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

            File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String pictureDirectoryPath = pictureDirectory.getPath();

            Uri data = Uri.parse(pictureDirectoryPath);
            photoPickerIntent.setDataAndType(data, "image/*");
            startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (resultCode == RESULT_OK) {

                if (requestCode == IMAGE_GALLERY_REQUEST) 
                    Uri imageUri = data.getData();

                    InputStream inputStream;

                    try {
                        inputStream = getContentResolver().openInputStream(imageUri);

                      } catch (FileNotFoundException e) {
                        e.printStackTrace()
                        Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }

I would have done it like below:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);


            if (resultCode == RESULT_OK) {

                if (requestCode == IMAGE_GALLERY_REQUEST) {
                    Uri imageUri = data.getData();

                    Long tsLong = System.currentTimeMillis();
                    String now = tsLong.toString();

                    String destitaion =folderPath+"/"+now+".jpg";

                    savefile(imageUri.getPath(),destitaion);
            }

        }
    }


    void savefile(String sourceFilename,String destinationFilename)
    {
        //String sourceFilename= sourceuri.getPath();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        boolean exp_occuered=false;
        try {
            bis = new BufferedInputStream(new FileInputStream(sourceFilename));
            bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
            byte[] buf = new byte[1024];
            bis.read(buf);
            do {
                bos.write(buf);
            } while(bis.read(buf) != -1);
        } catch (IOException e) {
            e.printStackTrace();
            exp_occuered=true;
        }
        finally {
            try {
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(!exp_occuered)
        {
            //the image has been saved properly
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM