本文介绍了获取图像的电流id被显示在寻呼机查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我做了寻呼机查看我的应用程序,但我有一个小问题。我无法弄清楚如何从我观看图像的同时获得ID。我ressources是资产的文件夹。是否有可能获得形象标识,并设置图片作为壁纸?
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();
}
});
}
}
推荐答案
而不是setResource请使用setBitmap或setStream
instead of setResource use either setBitmap or setStream
这篇关于获取图像的电流id被显示在寻呼机查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!