Get/pick an image from Android's built-in Gallery app programmatically

前端 未结 19 1151
终归单人心
终归单人心 2020-11-22 00:49

I am trying to open an image / picture in the Gallery built-in app from inside my application.

I have a URI of the picture (the picture is located on the SD card).

19条回答
  •  抹茶落季
    2020-11-22 01:24

    here is my example, might not be as your case exactly.


    assuming that you get base64 format from your API provider, give it a file name and file extension, save it to certain location in the file system.

    public static void shownInBuiltInGallery(final Context ctx, String strBase64Image, final String strFileName, final String strFileExtension){
    
    new AsyncTask() {
        @Override
        protected File doInBackground(String... strBase64Image) {
    
            Bitmap bmpImage = convertBase64StringToBitmap(strBase64Image[0], Base64.NO_WRAP);
    
            if(bmpImage == null) {
                cancel(true);
                return null;
            }
    
            byte[] byImage = null;
    
            if(strFileExtension.compareToIgnoreCase(FILE_EXTENSION_JPG) == 0) {
                byImage = convertToJpgByte(bmpImage); // convert bitmap to binary for latter use
            } else if(strFileExtension.compareToIgnoreCase(FILE_EXTENSION_PNG) == 0){
                byImage = convertToPngByte(bmpImage); // convert bitmap to binary for latter use
            } else if(strFileExtension.compareToIgnoreCase(FILE_EXTENSION_BMP) == 0){
                byImage = convertToBmpByte(bmpImage); // convert bitmap to binary for latter use
            } else {
                cancel(true);
                return null;
            }
    
            if(byImage == null) {
                cancel(true);
                return null;
            }
    
            File imageFolder = ctx.getExternalCacheDir();
    
            if(imageFolder.exists() == false){
                if(imageFolder.mkdirs() == false){
                    cancel(true);
                    return null;
                }
            }
    
            File imageFile = null;
    
            try {
                imageFile = File.createTempFile(strFileName, strFileExtension, imageFolder);
            } catch (IOException e){
                e.printStackTrace();
            }
    
            if(imageFile == null){
                cancel(true);
                return null;
            }
    
            if (imageFile.exists() == true) {
                if(imageFile.delete() == false){
                    cancel(true);
                    return null;
                }
            }
    
            FileOutputStream fos = null;
    
            try {
                fos = new FileOutputStream(imageFile.getPath());
                fos.write(byImage);
                fos.flush();
                fos.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            } finally {
                fos = null;
            }
    
            return imageFile;
        }
    
        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
    
                String strAuthority = ctx.getPackageName() + ".provider";
                Uri uriImage = FileProvider.getUriForFile(ctx, strAuthority, file);
    
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(uriImage, "image/*");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                ctx.startActivity(intent);
    
        }
    }.execute(strBase64Image);}
    

    Don't forget to set up a proper file provider at first place in AndroidManifest.xml

            
    
            
        
    

    where the file path is a xml in .../res/xml/file_path.xml

    
    

    
    
    
    
    
    
    
    
    
    
    
    
    


    Long story short, have file provider ready at first, pass Uri to Intent for known and accessible picture source, otherwise, save the picture in desired location and then pass the location (as Uri) to Intent.

提交回复
热议问题