我正在使用viewpager以全屏显示资源/可绘制文件夹中的图像。它工作正常,滑动得很好。现在我想在SD卡中保存图像名称,但我无法获取图像文件名。如果有人请帮助我。我有谷歌它,我没有得到正确的解决方案我的期望。
码
public class MainActivity extends Activity{
//private static Context context;
ExtendedViewPager mViewPager;
private static Uri imageUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = { R.drawable.nature_1, R.drawable.nature_2, R.drawable.nature_3, R.drawable.nature_4, R.drawable.nature_5 };
}
@Override
public int getCount() {
return images.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
//Context context = container.getContext();
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int imagid=images[position];
System.out.println("Image name " + images[position]);
bm = BitmapFactory.decodeStream(v.getResources().getDrawable(imagid));// here i need to pass my resId, i can't able to pass
}
});
//img.setScaleType(TouchImageView.ScaleType.CENTER_CROP);
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
答案 0 :(得分:2)
创建一个包含文件名的String[]
,并将其设置为img
作为标记,并在onClick
中获取标记。做这样的事
private static String[] imagesName = {"fiel1","file2",.....};
.
.
img.setImageResource(images[position]);
img.setTag(imagesName[position]);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String filename = v.getTag().toString();
}
}
修改强>
如果您不想创建String[]
,请将images[position]
设置为img的Tag
,然后在onClick
中将其设置为getResources().getResourceName((Integer)v.getTag());
< / p>
这有助于我。