简体   繁体   中英

Reading images from sd-card like res

I have mass of int, witch I use like this:

 final int[] iconN = new int[] { R.drawable.arrow_left_a,
   R.drawable.arrow_right_a, R.drawable.sound_on,
   R.drawable.sound_off };

To set image on click:

final ImageView bL = (ImageView) findViewById(R.id.iV1);
  bL.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
       bL.setImageResource(iconN[0]);
   }
  });

This files are in drowable. What I need to do when I'll work with files from sd-card the same way? I try to do something like this:

final int[] iconC = new int[] {
            Integer.parseInt(iA.getSdDir() + "/Files/Colors/arrow_left_a.png"),
            Integer.parseInt(iA.getSdDir() + "/Files/Colors/arrow_right_a.png") };

But it's wrong and I get an err:

 java.lang.NumberFormatException: unable to parse '/mnt/sdcard/izuchaika//Files/Colors/arrow_left_a.png' as integer

You're getting a numberFormatException because you're trying to parse the fully-qualified path name for a file as an integer. That won't be possible.

To use the image from the SD card, you have to read it into a bitmap and then pass that to the image view:

bl.setImageBitmap(BitmapFactory.decodeFile(iA.getSdDir()+"/Files/Colors/arrow_left_a.png"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM