开发者

Store image and then later display it in ImageView on Android

开发者 https://www.devze.com 2023-02-16 19:58 出处:网络
Im building an application on android that displays an image stored in a specific folder on the SD card. I want my code to build the folder on the SD card then put an image file in that folder. Then a

Im building an application on android that displays an image stored in a specific folder on the SD card. I want my code to build the folder on the SD card then put an image file in that folder. Then after it is created I want it to pull the image out of the folder and display it in the imageview. This 开发者_开发技巧is what i have so far.

    //create the folder then add the image here
    File SDlocation = Environment.getExternalStorageDirectory();
    File clothingdirectory = new File(SDlocation + "/images/");
    clothingdirectory.mkdirs();
    File outputFile = new File(clothingdirectory, imagename.png);
    FileOutputStream fos = new FileOutputStream(outputFile);

    //pull the image out of the folder here
    Bitmap bMap = BitmapFactory.decodeFile(SDLocation + "/images/imagename.png");
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bMap);

EDIT: sorry my question is how create the file to save on the phone. I'm missing a step. I don't have an image called imagename.png. How would i create one?

EDIT2: How do i take an image from my project folder and put in in the directory i created to pull out later?


Peter,

You can put the image in your drawable/ folder then you can set the ImageView with setImageDrawable(R.drawable.imagename).

0

精彩评论

暂无评论...
验证码 换一张
取 消