-1

Hello everyone I am creating an online wallpaper app.Where users can access wallpaper online.i want add two button first one is set as wallpaper and second one is download button .So if anybody help me about this I will be thankful to them thanks.

public class GalleryDetailActivity extends ActionBarActivity {

public static final String EXTRA_IMAGE = "extra_image";


private ImageView mImageView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    mImageView = (ImageView) findViewById(R.id.image);

    if (getIntent() != null && getIntent().getExtras() != null) {
        if (getIntent().getExtras().containsKey(EXTRA_IMAGE)) {
            Picasso.with(this).load(getIntent().getExtras().getString(EXTRA_IMAGE)).into(mImageView);
        }

    }

1 Answers1

0

First of all, you should make sure you have permission to make such action. Add in your manifest:

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

To download the image hosted in some site you could use a 'doInBackground Thread' as suggested below:

How to download and save an image in Android

The button to set the wallpaper is created by code below:

Button setWallpaper = (Button)findViewById(R.id.YOUR_BUTTON);
ImageView imagePreview = (ImageView)findViewById(R.id.YOUR_PREVIEW);
imagePreview.setImageResource(YOUR_IMAGE_RESOURCE);

setWallpaper.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(YOUR_IMAGE_RESOURCE);
            } catch (IOException e) {
                e.printStackTrace();
            }
}});

YOUR_IMAGE could be a local resource, like:

R.drawable.myImageFile

The link in the answer there are several ways of how to download your online image. Please, check it and try set up your wallpaper with local images first.

File f = new File(Environment.getExternalStorageDirectory(), "yourfile.jpg");
String path = f.getAbsolutePath();
File jpg = new File(path);

if(jpg.exists()) {
    Bitmap bmp = BitmapFactory.decodeFile(path);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
    WallpaperManager m=WallpaperManager.getInstance(this);

    try {
        m.setBitmap(bmp);
    } catch (IOException e) {
        e.printStackTrace();
    }
} 
Community
  • 1
  • 1
Igor Silva
  • 39
  • 4