How to getCropAndSetWallpaperIntent(Uri imageUri)

2020-03-03 08:09发布

问题:

So, there are two questions that are the same as this( How to use getCropAndSetWallpaperIntent method in WallpaperManager? AND How to use getCropAndSetWallpaperIntent? ), but there are no answers to both of them. In hope of an answer I'm asking this - how to get this method to work.

http://developer.android.com/reference/android/app/WallpaperManager.html#getCropAndSetWallpaperIntent(android.net.Uri) that doesn't help my brains.

And this doesn't work

WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
Uri uri = Uri.parse("android.resource://lv.revo.inspicfootballhd/drawable/v1");
Intent intent = new Intent(wallpaperManager.getCropAndSetWallpaperIntent(uri));
startActivity(intent);

logcat shows this

5891-5891/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: lv.revo.inspicfootballhd, PID: 5891
    java.lang.IllegalArgumentException: Image URI must be of the content scheme type
            at android.app.WallpaperManager.getCropAndSetWallpaperIntent(WallpaperManager.java:760)
            at lv.revo.inspicfootballhd.MainActivity.onTouch(MainActivity.java:236)
            at android.view.View.dispatchTouchEvent(View.java:7701)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2338)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2465)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2285)
            at android.view.View.dispatchPointerEvent(View.java:7886)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:138)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:5144)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)

So, according to documentation "The image URI that will be set in the intent. The must be a content URI and its provider must resolve its type to "image/*""

The image is in .jpg format. I'm going to keep finding a solution to this tomorrow. I just hoped that someone smarter knows the answer to my question.

EDIT/UPDATE 1: So I managed to change uri to Content Uri, I believe so. Did that using this - https://stackoverflow.com/a/23223556/2727408

Now it shows this

java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*
            at android.app.WallpaperManager.getCropAndSetWallpaperIntent(WallpaperManager.java:792)
            at lv.revo.inspicfootballhd.MainActivity.onTouch(MainActivity.java:244)

I tried to check the type using ContentResolver getType(contentURI), it returned null. Now I'll try to find how to change that to image/*

The code so far looks like this

Uri uri = ResourceToUri(getApplicationContext(), imageArray[j]);
File wallpaper_file = new File(uri.getPath());
Uri contentURI = getImageContentUri(getApplicationContext(), wallpaper_file.getAbsolutePath());

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

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

Thanks so far.

EDIT/UPDATE 2: Created new question about my new issue here: How to change type to image/*

回答1:

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?



回答2:

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