2014-01-09 60 views
2

因此,我爲我的應用程序製作了查看傳呼機,但是我遇到了一個小問題。我無法弄清楚如何從我正在查看的圖像中獲取ID。我的資源位於資產文件夾中。是否有可能獲得圖像的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(); 

    } 
}); 

}

}

+1

你可以使用viewPagerIndicator也爲此 – jyomin

回答

1

代替setResource請使用setBitmap或setStream

+0

我tryid這樣 WallpaperManager myWallpaperManager \t = WallpaperManager.getInstance(getApplicationContext())。 \t try { \t \t InputStream stream = getAssets()。open(「」); \t \t \t myWallpaperManager.setStream(stream); \t} catch(IOException e){ \t // TODO自動生成的catch塊 \t e.printStackTrace(); \t} 但顯示錯誤無法啓動活動ComponentInfo {com.testtouchviewpager/com.testtouchviewpager.MainActivity}:java.lang.NullPointerException – ArmsOP

+0

你想用什麼jpg作爲壁紙? – pskink

+0

資產文件夾中的圖像。他們被命名爲1.jpg,2.jpg等我試圖從頁面上的curent圖像獲取資源。 – ArmsOP

1

使用此:

myWallpaperManager.setResource(imageArray[curruntPosition]); 

代替

myWallpaperManager.setResource(R.drawable.ic_launcher); 
+0

所以我需要做我的資產資源的數組列表?如何做到這一點? – ArmsOP

+0

嘗試[this](http://stackoverflow.com/a/5545712/2185272)@ArmsOP –

+0

無法弄清楚:/ – ArmsOP