您的当前位置:首页正文

setImageBitmap

2024-11-06 来源:个人技术集锦
/**
     * 由本地文件路径、网络url或者项目的资源文件,生成Bitmap(旧,极端情况下可能造成OOM)
     * @param filePath
     */
    private void productBitmap(String filePath){
        Bitmap des_bitmap = ;
        BitmapFactory.Options options  = new BitmapFactory.Options();
//        options.inPreferredConfig
        //本地文件路径或者网络url
        Uri uri = Uri.parse(filePath);
        des_bitmap = BitmapFactory.decodeFile(uri.toString(),options);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">项目资源文件</span>
    des_bitmap =<span style="color: #000000;"> BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">流,例如文件流</span>
    FileInputStream fis = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        fis </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(filePath);
        des_bitmap </span>= BitmapFactory.decodeStream(fis,<span style="color: #0000ff;">null</span><span style="color: #000000;">,options);
        fis.close();
        fis </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
        e.printStackTrace();
    }</span><span style="color: #0000ff;">finally</span><span style="color: #000000;"> {
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            </span><span style="color: #0000ff;">if</span> (fis != <span style="color: #0000ff;">null</span><span style="color: #000000;">)
                fis.close();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {

        }
    }

    </span><span style="color: #0000ff;">if</span>(iv_bitmap_test!=<span style="color: #0000ff;">null</span><span style="color: #000000;">) {
        iv_bitmap_test.setImageBitmap(des_bitmap);
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        iv_bitmap_test </span>=<span style="color: #000000;"> (ImageView) findViewById(R.id.iv_bitmap_test);
        iv_bitmap_test.setImageBitmap(des_bitmap);
    }
}</span></pre>
Top