0

My app shows an image inside an Image View. I want users to select set picture as option from withing the app itself. Like this Set as option

How can I add "set as" popup menu so that users can directly set the image as wallpaper? Image is already stored in Drawable folder.

Jack Stern
  • 375
  • 1
  • 4
  • 15
  • Possible duplicate of [android set image as contact icon/wallpaper](http://stackoverflow.com/questions/7284142/android-set-image-as-contact-icon-wallpaper) – Ads Feb 24 '16 at 19:41
  • I tried to follow their methods but now I'm stuck [here](http://stackoverflow.com/questions/35619631/unable-to-set-picture-as-no-appps-can-perform-this-action) – Jack Stern Feb 25 '16 at 06:14

1 Answers1

0

You can use following code for "set picture as". Here I am taking the screenshot of the current activity. Then I am storing that activity as bitmap. After that I am providing that activity to intent for "Set As Option"

On button Click :

OnButtonClick(){
ImageProcessing imageProcessing = new ImageProcessing();
            Bitmap bitmap = imageProcessing.takeScreenshot(getWindow().getDecorView().findViewById(R.id.view_thought));
            imageProcessing.saveBitmap(bitmap);
            Intent intent = imageProcessing.setAsOption(this,imageProcessing.getSavedImagePath());
            startActivityForResult(Intent.createChooser(intent, "Set image as"), 200);
}

Implement a new class ImageProcessing

public class ImageProcessing {
private File imagesPath;
public void saveBitmap(Bitmap bitmap) {
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagesPath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("POS", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("POS", e.getMessage(), e);
    }
}
public File getSavedImagePath(){
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
    return imagesPath;
}

public Bitmap takeScreenshot(View rootView) {
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public Intent setAsOption(Context cntxt,File imagesPath){
    /*File imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");*/
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    if(imagesPath.exists()){
        Uri contentUri = FileProvider.getUriForFile(cntxt, BuildConfig.APPLICATION_ID+".Utility.GenericFileProvider",imagesPath);

        intent.setDataAndType(contentUri, "image/jpg");
        intent.putExtra("mimeType", "image/jpg");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }else {
        Toast.makeText(cntxt,"Not a wallpaper",Toast.LENGTH_SHORT).show();
    }
    return intent;
}

}

In menifest add :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Enigmatic Mind
  • 466
  • 6
  • 20