显示设置为壁纸或设置为联系人图片的意图

时间:2017-01-24 11:55:57

标签: android

我想将图片视图设置为墙纸或联系人图片。 我需要为用户打开“选择器”,我正在使用此代码:

Uri uri = Uri.parse("android.resource://"+ this.getPackageName()+"/drawable/" +name+"jpg" );
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/jpeg");
    intent.putExtra("mimeType", "image/jpeg");
    this.startActivity(Intent.createChooser(intent, "Set as:"));

这会产生以下消息: “没有应用可以执行此操作。”

2 个答案:

答案 0 :(得分:1)

use this code :

    Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
    startActivity(Intent.createChooser(intent, "Select Wallpaper"));
    You can create an item in contextMenu as Set as wallpaper in your app and when the user taps that item, you use this code.

    Call this method for setting the selected image as Wallpaper:

    public void setWallpaper() {

            Context context = this.getBaseContext(); 
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),mImageIds[pos]);
            context.setWallpaper(mBitmap);
    }
    And add this permission in Android Manifest file:

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

答案 1 :(得分:0)

现在大多数应用都无法识别“jpeg”图像。我建议您查找每种图像格式的应用程序。请使用以下代码:

Uri uri = Uri.parse("android.resource://"+ this.getPackageName()+"/drawable/" +name+"jpg" );
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("mimeType", "image/*");
    this.startActivity(Intent.createChooser(intent, "Set as:"));