获取在查看寻呼机上显示的图像的当前ID

时间:2014-01-09 09:07:50

标签: android android-viewpager

所以我为我的应用程序制作了视图寻呼机,但我遇到了一个小问题。我无法弄清楚如何从我正在查看的图像中获取ID。我的资源在资产文​​件夹中。是否可以获取图像的ID,并将该图像设置为壁纸?

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();

    }
});

}

}

2 个答案:

答案 0 :(得分:1)

使用此:

myWallpaperManager.setResource(imageArray[curruntPosition]);

而不是

myWallpaperManager.setResource(R.drawable.ic_launcher);

答案 1 :(得分:1)

而不是setResource使用setBitmap或setStream