Android - downloading image from web, saving to internal memory in location private to app, displaying for list item

后端 未结 3 1365
Happy的楠姐
Happy的楠姐 2021-02-06 11:35

What I\'m trying to do is this: I want my application to download an image from the Internet and save it to the phone\'s internal memory in a location that is private to the app

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 12:10

    void  writeToFile(Bitmap _bitmapScaled)
        {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            _bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
            try{
            File f;
            //you can create a new file name "test.jpg" in sdcard folder.
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                f =new File(android.os.Environment.getExternalStorageDirectory(),"FamilyLocator/userimages/"+imageName);
            else
             f = new File(Environment.getDataDirectory(),"FamilyLocator/userimages/"+imageName);
    
            f.createNewFile();
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            // remember close de FileOutput
            fo.close();
            }catch(Exception e){e.printStackTrace();}
        }
    

提交回复
热议问题