如何使用应用程序服务 Wallpaper 和 Contact photo 设定壁纸?

3
我使用下面的代码,但仅在我的真实设备api23 nexus5上部分工作,而在其他设备上无法设置。也不能为联系人图标设置壁纸。
我的操作:
- 点击设置壁纸按钮 - 在打开的窗口中选择服务:
如果选择“联系人照片” - 打开该服务并选择任何一个联系人。
实际结果:只是返回到我的壁纸应用程序而没有进行设置。
期望结果:必须打开“裁剪图片”图像,然后点击将此图像设置为联系人图标。
如果选择“壁纸”
  • 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:"));
    
为什么无法工作?
1个回答

1
我解决了这个问题。
相反,使用 setAs.setDataAndType(Uri.fromFile(mFile), "image/*"); 就可以了,不要使用 intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "image/*");
全部代码如下:
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:"));
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接