尝试使用Universal Image Loader获取当前图像并设置为墙纸

时间:2013-09-28 16:15:47

标签: java android wallpaper universal-image-loader

我正在尝试实现用户浏览我的应用程序将图像设置为壁纸的功能。我正在使用通用图像加载器,除了设置壁纸时,一切都很好。我已尝试过Stackoverflow和其他网站的几种不同方法,但没有成功。我最后一次尝试使用此处的解决方案: Android set image as wallpaper

实现之后一切运行正常,直到我选择调用setWall的按钮(setwallbtn)。那时我在我的logcat中遇到了一堆错误(在代码之后,下面发布了完整的logcat错误)。

下面是pageview_item.xml和ImagePagerActivity.java。

任何人都能提供的帮助将不胜感激!

谢谢!

pageview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="1dip" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/descr_image" />

    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/setwallbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/setwall"
        android:maxHeight="55dp"
        android:onClick="setWall"
        android:src="@drawable/setwallpaper" />

</FrameLayout>

imagepageractivity.java

import java.io.IOException;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.rwilco12.wallpapergallery.Constants.Extra;

/**
 * @original author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class ImagePagerActivity extends BaseActivity {

    private static final String STATE_POSITION = "STATE_POSITION";

    DisplayImageOptions options;

    ViewPager pager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pageview);

        Bundle bundle = getIntent().getExtras();
        String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
        int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.error)
            .showImageOnFail(R.drawable.error)
            .resetViewBeforeLoading()
            .cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .resetViewBeforeLoading()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .build();

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(imageUrls));
        pager.setCurrentItem(pagerPosition);
    }

     //Begin code from: https://stackoverflow.com/questions/11938182/android-set-image-as-wallpaper
     // fetch bitmap from view
    public static Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            // if we unable to get background drawable then we will set white color as wallpaper
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }

    public void setWall(int i) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            // below line of code will set your current visible pager item to wallpaper
            // first we have to fetch bitmap from visible view and then we can pass it to wallpaper
            myWallpaperManager.setBitmap(getBitmapFromView(pager.getChildAt(1)));

            // below line of code will set input stream data directly to wallpaper
            // myWallpaperManager.setStream(InputStream Data);

            // below line of code will set any image which is in the drawable folder 
            // myWallpaperManager.setResource(R.drawable.icon);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //End code from: https://stackoverflow.com/questions/11938182/android-set-image-as-wallpaper


    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, pager.getCurrentItem());
    }

    private class ImagePagerAdapter extends PagerAdapter {

        private String[] images;
        private LayoutInflater inflater;

        ImagePagerAdapter(String[] images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return images.length;
        }





        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.pageview_item, view, false);
            ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                            break;
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }
                    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }
    }

logcat错误

09-28 10:01:36.334: W/dalvikvm(26948): threadid=1: thread exiting with uncaught exception (group=0x420772a0)
09-28 10:01:36.334: E/AndroidRuntime(26948): FATAL EXCEPTION: main
09-28 10:01:36.334: E/AndroidRuntime(26948): java.lang.IllegalStateException: Could not find a method setWall(View) in the activity class com.rwilco12.wallpapergallery.ImagePagerActivity for onClick handler on view class android.widget.ImageView with id 'setwallbtn'
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$1.onClick(View.java:3686)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View.performClick(View.java:4223)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$PerformClick.run(View.java:17281)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Handler.handleCallback(Handler.java:615)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Looper.loop(Looper.java:137)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:106)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at dalvik.system.NativeStart.main(Native Method)
09-28 10:01:36.334: E/AndroidRuntime(26948): Caused by: java.lang.NoSuchMethodException: setWall [class android.view.View]
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.Class.getMethod(Class.java:915)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$1.onClick(View.java:3679)
09-28 10:01:36.334: E/AndroidRuntime(26948):    ... 12 more

1 个答案:

答案 0 :(得分:0)

尝试设置onclick以匹配lolcat想要的内容

类似的东西:

public void setWall(View v) {
    WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    try {
        // below line of code will set your current visible pager item to wallpaper
        // first we have to fetch bitmap from visible view and then we can pass it to wallpaper
        myWallpaperManager.setBitmap(getBitmapFromView(v));

    } catch (IOException e) {
        e.printStackTrace();
    }
}