質問

i'd like to get filename of shuffled ImageViews

    private List<Drawable> images_;
    images_ = new ArrayList<Drawable>();
    images_.clear();

    images_.add(getResources().getDrawable(R.drawable.img1));
    images_.add(getResources().getDrawable(R.drawable.img2));
    images_.add(getResources().getDrawable(R.drawable.img3));
    images_.add(getResources().getDrawable(R.drawable.img4));

    Collections.shuffle(images_); 

    ImageView img_1 = (ImageView)findViewById(R.id.img_1);   
    img_1.setBackgroundDrawable(images_.get(0));

How to know what image name was setted in this ImageView (img_1) ?

役に立ちましたか?

解決

Use setImageResource(resID) instead of setBackgroundDrawable. Now you can get the id of the image as the same resID using the setId(resID) Method. To get the image name use the following method.

getResources().getResourceName(urImage.getId());


ArrayList<Integer> list = new ArrayList<Integer>() {{
    add(R.drawable.img1);
    add(R.drawable.img2);
    add(R.drawable.img3);
}}
Collections.shuffle(list);
ImageView img_1 = (ImageView)findViewById(R.id.urimageId);   
img_1.setImageResource(list.get(0));
img_1.setTag(list.get(0));

Now to get the image name do the following.

getResources().getResourceName((Integer)img_1.getTag());

他のヒント

According to the Android Developers website the ImageView is inherited from android.view.View. So you can use getResources() to get a Resources object and then use getAssets() to get the AssetManager.

I'm at work so I can't provide example right now. Sorry. I hope this helps though.

Happy coding! :)

Try this to get file name from an ImageView:

ImageView iv = new ImageView(MainActivity.this);
String path = f.getAbsolutePath();                 
iv.setImageBitmap(BitmapFactory.decodeFile(path));
iv.setTag(path);

Retrieving the path:

String path = (String) iv.getTag();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top