How to Copy Image File from Gallery to another folder programmatically in Android

前端 未结 5 1071
北荒
北荒 2020-12-08 08:50

I want to pick image from gallery and copy it in to other folder in SDCard.

Code to Pick Image from Gallery

Intent photoPickerIntent = new Intent(Int         


        
5条回答
  •  余生分开走
    2020-12-08 09:08

    one solution can be,

    1) read bytes from inputStream of the picked file.

    i get "content://media/external/images/media/681" this URI onActivityResult. You can get the file name by querying this Uri u got. get inputStream of it. read it into byte[].

    here you go/

    Uri u = Uri.Parse("content://media/external/images/media/681");

    Cursor cursor = contentResolver.query(u, null, null, null, null); there is a column name "_data" which will return you the filename, from filename you can create inputstream,

    you can now read this input stream

             byte data=new byte[fis.available()];
              fis.read(data);
    

    So you have data(byte array) with images byte

    2) create a file on to sdcard, and write with byte[] taken in step one.

           File file=new File(fileOnSD.getAbsolutePath() +"your foldername", fileName);
            FileOutputStream fout=new FileOutputStream(file, false);
            fout.write(data);
    

    as fileName you already have from the query method, use same here.

提交回复
热议问题