Search code examples
androidandroid-intentbitmapintentservicewallpaper

How to set wallpapers use app-services Wallpaper and Contact photo?


I use this code below, but it works partly - set wallpapers only on my real device api23 nexus5, in another devices in no way not setting. Also cant set wallpapers to icon of contact.

My actions:

  • tap button set wallpaper
  • in the opened window select service:

if choosed 'Contact photo' - open the service and if choose any contact

  • Actual result: just return to my wallpapers app without set
  • Expected result: must open 'crop picture' the image and then tap to set this image to contact icon.

if choosed 'Wallpaper'

  • Actual reuslt: just return to my wallpapers app without set and show message 'Can not load the image'(work only api 23 on my Nexus5)
  • Expected result: open the service and tap to set

            Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); // attach services
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            file = new File(getFolderStorageDirectory(), getFileName()); // create temp file
            if (isExternalStorageWritable()) { // check whether available external storage
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); // write image
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, "File not saved");
                }
            } else {
                showToast(getString(R.string.sd_card));
            }
            intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.putExtra("jpg", "image/*");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath()));
            startActivity(Intent.createChooser(intent, "Select service:"));
    

Why not work?


Solution

  • I solved this problem.

    Instead

    intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "image/*");

    use

    setAs.setDataAndType(Uri.fromFile(mFile), "image/*"); and it will work.

    All code:

    private void setAsUseServices() {
            onCreateFileListener(); // here will create file e.g. in Picture directory
            Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
            setAs.addCategory(Intent.CATEGORY_DEFAULT);
            Uri sourceUri = Uri.fromFile(mFile);
            setAs.setDataAndType(sourceUri, "image/*");
            setAs.putExtra("mimeType", "image/*");
            setAs.putExtra("save_path", sourceUri);
            startActivity(Intent.createChooser(setAs, "Select service:"));
        }