android图片显示的几种办法

来源:互联网 发布:mac os什么时候更新 编辑:程序博客网 时间:2024/05/21 09:51

1、使用imageView控件函数

共同优点:使用方便

ImageView.setImageBitmap(Bitmap b);

缺点:bitmap过大时会抛出异常。

ImageView.setImageResource(int id):

缺点:只能调用Drawable中的图片,图片对不同分辨率有要求,同一张图片在不同的Drawable分辨率文件夹中显示效果不同


2、将Bitmap资源放在Assets文件夹下

系统在编译的时候不会编译assets下的资源文件,assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。

具体操作方式:

       try { 
<span style="white-space:pre"></span>   //使用AssestManager将文件已字节流读入内存                InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
           BufferedReader bufReader = new BufferedReader(inputReader);                String line="";                String Result="";                while((line = bufReader.readLine()) != null)                    Result += line;                return Result;            } catch (Exception e) {                 e.printStackTrace();             }

3、使用Intent从相机、相册等获取本地文件的Uri

int request_code;//大于等于零

...

protected void onClick(View v)

{

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);//调用系统相册

startActivityForResult(intent,request_code);

}

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

  super.onActivityResult(requestCode, resultCode, intent);
 Uri imageFileUri;
  if (resultCode == RESULT_OK) {
  switch (requestCode) {

...
   case request_code:
    imageFileUri = intent.getData();//获取选择图片的URI
    break;

  }


4、使用canvas将Bitmap“画”在屏幕上

优点:非常灵活,可以通过遮罩效果在不规则图形区域内(外)显示图片,调整图片位置

缺点:Bitmap在内存中占用大量的内存,使用时需要根据功能选取使用

Uri imageFileUri;
0 0
原创粉丝点击