zoukankan      html  css  js  c++  java
  • bitmap的用法(浅谈)

    需求:从服务器下载一张图片,显示在ImageView控件上,并将该图片保存在移动设备的SD上。
    步骤:
    (一)获得输入流
                    //urlPath:服务器路径;
            public InputStream getUrlInputStream(String urlPath) throws IOException{
                    URL url=new URL(urlPath);
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    InputStream in=conn.getInputStream();
                    if(in!=null){
                            return in;
                    }else{
                            Log.i("test", "输入流对象为空");
                            return null;
                    }
            }
    (二)将输入流转化为Bitmap流
    public Bitmap getMyBitmap(InputStream in){
                    Bitmap bitmap=null;
                    if(in!=null){
                            bitmap=BitmapFactory.decodeStream(in);
                            //BitmapFactory的作用:create Bitmap objects from various sources,including files,streams and byte-arrays;
                            return bitmap;
                    }else{
                            Log.i("test", "输入流对象in为空");
                            return null;
                    }
            }
    (三)给ImageView对象赋值
    public void setWidgetImage(Bitmap bitmap){
                    ImageView img=new ImageView(this);
                    if(bitmap!=null){
                            img.setImageBitmap(bitmap);
                    }
            }
    (四)获取SD卡上的文件存储路径
    public void createSDFile(){
                    File sdroot=Environment.getExternalStorageDirectory();
                    File file=new File(sdroot+"/Android/date/包名/文件名");
                    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                            /**
                             * 相关操作
                             */
                    }
            }

    第四步不是很很完美,File file=new File(sdroot+"/Android/date/包名/文件名");明显很麻烦,补一个方法如下:

    public void getFileInfo(){
    File skRoot = Environment.getExternalStorageDirectory();
    File fileRoot = MainActivity.this.getFilesDir();
    Log.i(tag + "判断SD卡是否插入", ""+Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED));
    Log.i(tag + "获得sd卡根目录", ""+skRoot.getAbsolutePath());
    Log.i(tag + "获得私有根目录", ""+fileRoot.getAbsolutePath());
    Log.i(tag + "获得sd卡根目录文件或文件夹的名称", ""+skRoot.getName());
    Log.i(tag + "获得私有根目录文件或文件夹的名称", ""+fileRoot.getName());
    }

    这样不管是得到程序的私有目录还是得到sd卡的目录就都有了,然后return就可以了

    ps:if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))判断sd卡是否插上

    注意:SD卡的权限
    (五)将图片保存到SD卡上
    public boolean readToSDCard(File file,Bitmap bitmap) throws FileNotFoundException{
                    FileOutputStream os=new FileOutputStream(file);
                    return bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
                    //bitmap.compress()的作用:write a compressed version of the bitmap to the specified outputstream;
                    //true:表示操作成功,false:表示操作失败
            }

  • 相关阅读:
    interface——关于虚函数的内存分配
    [asp.net]网页与服务器的交互模型
    C#下把txt文件数据读进sql server中存储所遇到的乱码问题
    安装window 7 续 ~~~
    ASP.NET中对SQLITE数据库进行插入操作后返回自增的ID
    非IE浏览器下让界面变灰色
    ASPNET目录权限控制
    自己封装的Access数据库的操作类(AccessHelper)
    用WIN7发现的问题(AppHangB1)
    [原创视频]牛腩新闻发布系统(续)
  • 原文地址:https://www.cnblogs.com/jh5240/p/2316642.html
Copyright © 2011-2022 走看看