android - 页面显示andriod系统目录下图片
阿神
阿神 2017-04-17 17:44:08
0
2
445

从服务器下载图片到本地路径,然后在前端展示那个图片,路径这块应该怎么处理?求大神支招啊。

阿神
阿神

闭关修行中......

reply all(2)
Peter_Zhu

1. Create a folder to store pictures
2. Create a file to store pictures
3. Download pictures and write them into files

//一般图片存储在手机SD卡 获取手机的SD卡根目录增加temp的文件夹路径
Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp";

Download images and store them locally in main code

void doSaveImage(){
        String filePath = FileUtil.getEnvironmentPath();
        Log.i("doSaveImage",filePath + "");
        File fileFloder = new File(filePath);
        if(!fileFloder.exists()){
            if(fileFloder.mkdirs()){
                Log.i("doSaveImage","file.mkdirs"  + " true");
            }else{
                Log.i("doSaveImage","file.mkdirs"  + " false");
                Toast.makeText(this,"save error!",Toast.LENGTH_SHORT).show();
            }
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            Log.i("doSaveImage",AppConst.IMG_HEAD_URL + mUrl);
            URL url = new URL(AppConst.IMG_HEAD_URL + mUrl);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(5*1000);
            int contentLength = con.getContentLength();
            in = con.getInputStream();
            byte[] bytes = new byte[1024];
            File file = new File(fileFloder.getPath(), name +".jpg");
            out = new FileOutputStream(file);
            int len;
            while ((len = in.read(bytes)) != -1){
                Log.i("doSaveImage",len + "");
                out.write(bytes,0,len);
            }
        } catch (Exception  e) {
            e.printStackTrace();
        }finally {
            try{
                if(in != null) in.close();
                if(out != null) out.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
洪涛

String path = null;
if (getExternalCacheDir() != null) {//Determine whether external storage is available, that is, SD card
//If there is one, use SD card#🎜🎜 # path = context.getExternalCacheDir().getAbsolutePath() + File.separator;
} else {
//Otherwise there is the phone’s own memory
path= context.getCacheDir().getAbsolutePath( ) + File.separator;
}

The above only writes to the root directory, you can create new directories according to your needs

getExternalCacheDir method context can be used directly in Application, or getExternalCacheDir can be called directly in Application

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template