0

I am following this answer to accomplish my task. Everything works well except the last part,

if(result!=null)
{

    Toast.makeText(getApplicationContext(), "Image saved in Gallery !", Toast.LENGTH_LONG).show();

    if(isinint) //check if any app cares for the result
    {
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.fromFile(new File(result.toString()))); //Create a new intent. First parameter means that you want to send the file. The second parameter is the URI pointing to a file on the sd card. (openprev has the datatype File)

        ((Activity) ImageListActivity.this).setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
        ((Activity) ImageListActivity.this).finish(); //close your application and get back to the requesting application like GMail and WhatsApp
        return; //do not execute code below, not important
    }
}

At last nothing crash but wallpaper is also not set in whatsapp.

Can anyone please let me know why it is not working?

Any help is appriciated.

Thanks

Community
  • 1
  • 1
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
  • Where do you set isinint? Did you debug the code and checked at what point it stops working? Please edit your question and give more informations. – James Cameron Mar 21 '15 at 11:45
  • @JamesCameron Yes, I already debug it, it executes all the lines, no error & then I go to whatsapp chat where wallpaper doesnt set – Vivek Warde Mar 30 '15 at 03:09
  • Found an answer yet? I imagine that the ACTION_SEND Intent may be wrong as it may be called with another intention. Please debug the Intent you get when the app is called. – James Cameron Jul 15 '15 at 13:16
  • @JamesCameron No, If you solved it pls answer here, Thanks – Vivek Warde Jul 20 '15 at 12:44

1 Answers1

0

You can use the following code..

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);
}

And 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;
}

}

And 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" />
Amit Sinha
  • 566
  • 7
  • 22