Set image as Background Onclick Android

I am working on a project in which click on an image from gridview opens that image in an ImageView , i got this code from this website http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ , so i modified the FullImageActivity.java and added a button to set the image as background, but everytime i click on the button, it just force closes my app, P.S i have added the permission in the manifest also

here is my code

package com.example.androidhive;

import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

 public class FullImageActivity extends Activity {

 private WallpaperManager imageView;


@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);
    }


 public void setaswall(View view) { // SET AS WALLPAPER BUTTON
    // TODO Auto-generated method stub
     WallpaperManager myWallpaperManager
      = WallpaperManager.getInstance(getApplicationContext());
     try {
      Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
      if(bitmap!=null)
      myWallpaperManager.setBitmap(bitmap);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

 }
 };

Answers


I think you may also be trying to use it like this:

private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    mImageView = (ImageView) findViewById(R.id.full_image_view);
    mImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageAdapter.mThumbIds[position]));
}

public void setaswall(View view) {
    WallpaperManager wm = WallpaperManager.getInstance(this);
    try {
        final Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
        if (bitmap != null) {
            wm.setBitmap(bitmap);
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

And make sure you've included the set wallpaper permission in your AndroidManifest.

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

Here you declare a field with the name "imageView", but of type WallpaperManager:

private WallpaperManager imageView;

then later you declare some local imageView:

ImageView imageView = (ImageView) findViewById(R.id.full_image_view);

last not least you are trying to get a Drawable from the field imageView that you have never initialized:

Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();

This will throw an exception which you later catch and print the stacktrace

==> Solution: initialize the field imageView properly and don't create local var that hides the field.


It is simple I think you may forgot add the Permission in your Mainfest file you

just add this in your Mainfest file

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

Need Your Help

How to make a php script to create a html file?

php file fopen

Well.... I want to know how to make a script to create a html page. So when the script is executed, it will create a new page. Something like this: mydomain.com/test.html . The script should create...

Phonegap Error: "An error occurred while listing Android targets" while installing Phonegap on Ubuntu

cordova phonegap-plugins cordova-3 cordova-plugins

http://cordova.apache.org/docs/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide