Autore Topic: Settare Wallpaper da una Gallery con Imageview  (Letto 638 volte)

Offline SgtBozzer

  • Nuovo arrivato
  • *
  • Post: 8
  • Respect: 0
    • Mostra profilo
Settare Wallpaper da una Gallery con Imageview
« il: 10 Febbraio 2012, 12:50:54 CET »
0
Ciao a tutti!
Sono di nuovo qui a chiedervi aiuto.
Ho una gallery che prende le immagini dal SD Card, seguendo un determinato percorso.
Al click le visualizza nella ImageView sottostante.

Al layout ho aggiunto un buttone che dovrebbe servire per impostare l'immagine nella ImageView come Wallpaper, ma non  capisco come fare.

Il codice è questo:
Codice (Java): [Seleziona]
public class mwallpaper extends Activity {
               

    public String[] myRemoteImages = {
        //"/sdcard/Multimedia/Immagini/image_LARGE_2.unknown",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/1.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/2.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/3.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/4.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/5.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/6.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/7.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/8.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/9.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/10.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/11.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/12.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/13.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/14.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/15.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/16.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/17.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/18.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/19.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/20.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/21.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/22.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/23.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/24.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/25.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/26.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/27.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/28.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/29.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/30.jpg",

};
   
   
ImageView imageView;
   
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_wallpaper);
   
   
    Gallery ga = (Gallery)findViewById(R.id.Gallery);
    ga.setAdapter(new ImageAdapter(this));
   
    imageView = (ImageView)findViewById(R.id.GalleryView);
    ga.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                           
                       
                    String myJpgPath;
                           
                    myJpgPath = myRemoteImages[arg2];
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize = 1;
                            Bitmap bm1 = BitmapFactory.decodeFile(myJpgPath, options);
                            imageView.setImageBitmap(bm1);
                    }
           
    });
   
   
}



public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
                    ctx = c;
                    TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
                    imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
                    ta.recycle();
                    }
   
            public String[] myRemoteImages1 = {
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/1.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/2.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/3.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/4.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/5.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/6.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/7.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/8.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/9.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/10.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/11.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/12.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/13.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/14.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/15.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/16.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/17.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/18.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/19.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/20.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/21.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/22.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/23.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/24.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/25.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/26.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/27.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/28.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/29.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/30.jpg",
            };
           
    /** Returns the amount of images we have defined. */
     public int getCount() { return this.myRemoteImages1.length; }
   
     public Object getItem(int position) { return position; }
     public long getItemId(int position) { return position; }

     @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            String myJpgPath;
                    myJpgPath = myRemoteImages[position];
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    Bitmap bm1 = BitmapFactory.decodeFile(myJpgPath, options);
                    iv.setImageBitmap(bm1);
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    iv.setLayoutParams(new Gallery.LayoutParams(150,120));
                    iv.setBackgroundResource(imageBackground);
            return iv;
    }
     
}

}

Datemi un mano please!!  :-) :-)

Offline SgtBozzer

  • Nuovo arrivato
  • *
  • Post: 8
  • Respect: 0
    • Mostra profilo
Re:Settare Wallpaper da una Gallery con Imageview
« Risposta #1 il: 10 Febbraio 2012, 22:05:26 CET »
0
Ok ho fatto dei passi avanti...
 
Codice (Java): [Seleziona]
public class mwallpaper extends Activity implements OnClickListener {
               
    Button set;
    Bitmap bitmap;

    public String[] myRemoteImages = {
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/1.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/2.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/3.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/4.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/5.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/6.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/7.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/8.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/9.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/10.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/11.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/12.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/13.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/14.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/15.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/16.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/17.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/18.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/19.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/20.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/21.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/22.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/23.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/24.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/25.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/26.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/27.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/28.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/29.jpg",
            "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/30.jpg",

};
   
   
ImageView imageView;
private int position;

   
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_wallpaper);
    set = (Button) findViewById(R.id.set);
    set.setOnClickListener(this);
   
   
    Gallery ga = (Gallery)findViewById(R.id.Gallery);
    ga.setAdapter(new ImageAdapter(this));
   
    imageView = (ImageView)findViewById(R.id.GalleryView);
    ga.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                           
                       
                    String myJpgPath;
                           
                    myJpgPath = myRemoteImages[arg2];
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize = 1;
                            Bitmap bm1 = BitmapFactory.decodeFile(myJpgPath, options);
                            imageView.setImageBitmap(bm1);
                    }
           
    });
   
}


    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.set:
            String myJpgPath;
           
            myJpgPath = myRemoteImages[position];
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 1;
                    Bitmap bitmap = BitmapFactory.decodeFile(myJpgPath, options);      
            try {
                getApplicationContext().setWallpaper(bitmap);
                Toast.makeText(mwallpaper.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(mwallpaper.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
            }

            // --------------- //

            break;
        }
        }
   




public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
                    ctx = c;
                    TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
                    imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
                    ta.recycle();
                    }
   
            public String[] myRemoteImages1 = {
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/1.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/2.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/3.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/4.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/5.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/6.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/7.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/8.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/9.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/10.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/11.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/12.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/13.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/14.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/15.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/16.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/17.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/18.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/19.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/20.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/21.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/22.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/23.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/24.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/25.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/26.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/27.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/28.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/29.jpg",
                    "/sdcard/Android/data/com.jotabout.zipdownloader/files/unzipped/30.jpg",
            };
           
    /** Returns the amount of images we have defined. */
     public int getCount() { return this.myRemoteImages1.length; }
   
     public Object getItem(int position) { return position; }
     public long getItemId(int position) { return position; }

     @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            String myJpgPath;
                    myJpgPath = myRemoteImages[position];
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    Bitmap bm1 = BitmapFactory.decodeFile(myJpgPath, options);
                    iv.setImageBitmap(bm1);
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    iv.setLayoutParams(new Gallery.LayoutParams(150,120));
                    iv.setBackgroundResource(imageBackground);
            return iv;
    }
     
}

}

Il problema che facendo così.. il bottone imposta come wallpaper sempre la prima immagine della gallery, invece vorrei che impostasse l'immagine presente nella imageview..
ho bisogno di una mano! :D