Question

i have bitmap images and when i going to set as wallpaper it is blur and its not fit with screen..how to set with proper screen dimension and how to get screen size to set it with images...how to solve this problem..

class MyPagerAdapter extends PagerAdapter {
    int breadset;
    int forwall;
    public int[] images1 = { R.drawable.s_1, R.drawable.s_2, R.drawable.s_3 };
    public int[] images2 = { R.drawable.s_5, R.drawable.s_6, R.drawable.s_4 };

    public MyPagerAdapter(int gotbread) {
        this.breadset = gotbread;
        // TODO Auto-generated constructor stub
    }

    public int setcall() {
        return forwall;

    }

    public int getCount() {
        return images1.length;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;

        view = inflater.inflate(R.layout.layout_fullscreen_view, null);
        ((ViewPager) collection).addView(view, 0);
        Button btn = (Button) view.findViewById(R.id.wallb);
        ImageView iv = (ImageView) view.findViewById(R.id.imgDisplay);

        switch (breadset) {
        case 0:
            iv.setImageResource(images1[position]);
            break;
        case 1:
            iv.setImageResource(images2[position]);
        }
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    switch (breadset) {
                    case 0:
                        myWallpaperManager.setResource(images1[forwall]);

                        break;
                    }
                    Context context = getApplicationContext();
                    CharSequence text = "wallpaper has been set to your screen!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // TODO Auto-generated method stub

            }

        });

        return view;
    }

    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

}
Was it helpful?

Solution

You can use this function:

public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 3;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    mPhoto = BitmapFactory.decodeFile(filePath, o2);
    myBitmap = ExifUtils.rotateBitmap(filePath, mPhoto);

    image.setImageBitmap(myBitmap);
}

You can also go with this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top