2

So I made view pager for my app, but I have a small problem. I can't figure out how to get ID from the image I am viewing. My ressources are in Assets Folder. Is it possible to get ID of image, and set that image as wallpaper?

public class MainActivity extends Activity {

private GalleryViewPager mViewPager;

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




    String[] urls = null;
    List<String> items = new ArrayList<String>();
    try {
        urls = getAssets().list("");

        for (String filename : urls) 
        {
            if (filename.matches(".+\\.jpg")) 
            {
                String path = getFilesDir() + "/" + filename;
                copy(getAssets().open(filename), new File(path) );
                items.add(path);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {
            Toast.makeText(MainActivity.this, "Current item is " + currentPosition, Toast.LENGTH_SHORT).show();
        }
    });

    mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);
}

public void copy( final InputStream in, File dst) throws IOException {

    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();

Button buttonSetWallpaper = (Button)findViewById(R.id.setwallpaper);
buttonSetWallpaper.setOnClickListener(new OnClickListener() {

    @Override
     public void onClick(View v) {
        // TODO Auto-generated method stub
        WallpaperManager myWallpaperManager
         = WallpaperManager.getInstance(getApplicationContext());
        try {
            InputStream In = getAssets().open("2.jpg");

         myWallpaperManager.setStream(In);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        Toast.makeText(getBaseContext(), "changed+", Toast.LENGTH_SHORT).show();

    }
});

}

}

BM84-M1
  • 111
  • 14

2 Answers2

1

Use this:

myWallpaperManager.setResource(imageArray[curruntPosition]);

instead of

myWallpaperManager.setResource(R.drawable.ic_launcher);
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

instead of setResource use either setBitmap or setStream

pskink
  • 23,874
  • 6
  • 66
  • 77
  • i tryid like this WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); try { InputStream stream = getAssets().open(""); myWallpaperManager.setStream(stream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } but show error Unable to start activity ComponentInfo{com.testtouchviewpager/com.testtouchviewpager.MainActivity}: java.lang.NullPointerException – BM84-M1 Jan 09 '14 at 14:36
  • what jpg are you trying to use as a wallpaper? – pskink Jan 09 '14 at 14:42
  • the images from the assets folder. they are named 1.jpg, 2.jpg etc i am trying take resources from curent image on the page. – BM84-M1 Jan 09 '14 at 14:44
  • it work i tryed like this [link](http://imgur.com/YP0395o) but now will set only that image 2.jpg – BM84-M1 Jan 09 '14 at 15:08
  • can you explain me how to set .open("...path...")? – BM84-M1 Jan 09 '14 at 21:03
  • Yeah but it always open same image, that is 2.jpg. I want that ID to change when I slide and open and start previewing new image. So, I want it to change ID when the image on screen is changed, and get ID from the picture on screen. – BM84-M1 Jan 09 '14 at 21:17
  • so add the path to the adapter you are using – pskink Jan 09 '14 at 21:23