How to getCropAndSetWallpaperIntent(Uri imageUri) to work?

…衆ロ難τιáo~ 提交于 2019-11-30 21:33:26
Tushar

Like the error says, you need a content URI. Content URIs allow you to share files with temporary read and write permissions.

Check out: Get a Content URI from a File URI?

File wallpaper_file = new File(uri.getPath());
 Uri contentURI = getImageContentUri(getApplicationContext(),wallpaper_file);

 ContentResolver cr = this.getContentResolver();
 Log.d("CONTENT TYPE: ", "IS: " + cr.getType(contentURI));

 Intent intent = new Intent(wallpaperManager.getCropAndSetWallpaperIntent(contentURI));
startActivity(intent);

public static Uri getImageContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Images.Media._ID},
            MediaStore.Images.Media.DATA + "=? ",
            new String[]{filePath}, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!