Download within app [closed]

1

My application and set of images, I would like to know how I can create a button that allows you to save the images on the phone or put the images as the background of the phone.

    
asked by anonymous 03.01.2017 / 01:54

1 answer

3

Good night, well, if you get the Bitmap, you can easily save it to the gallery using 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 your manifest

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

and for the android request:

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

adding 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();
}
    
03.01.2017 / 03:10