Download inside the app

Asked

Viewed 68 times

1

My app and set of images, I wonder how I can create a button to save the images on the phone or put the images as the phone background.

1 answer

3


Good evening, good, if you catch the bitmap, it is possible to save it easily in the gallery using the MediaStore

MediaStore.Images.Media.insertImage(getContentResolver(), 
        <SEU_BITMAP>, <TITULO> , <DESCRICAO>);

however, in android 5.0 onwards it is necessary to ask for permission before, in addition to putting in its manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and for the request on android:

 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE)

adding up everything:

final int REQUEST_CODE = 110;
public void salvaImagem(){
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        MediaStore.Images.Media.insertImage(getContentResolver(), 
                <SEU_BITMAP>, <TITULO> , <DESCRICAO>);
    }else{
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
    }
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED && requestCode == REQUEST_CODE){
        salvaImagem();
}
  • Thank you so much for your help, I’m starting in this area.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.