导航:首页 > 操作系统 > android读取图片的bitmap

android读取图片的bitmap

发布时间:2022-06-16 15:05:51

android 读取Bitmap的几种方式

想读取本地项目里的资源图片,但又不能用到R文件。查了很多资料终于找到了。 现总结以下几种读取Bitmap的方法。 1.以文件流的方式,假设在sdcard下有 test.png图片 FileInputStream fis = new FileInputStream("/sdcard/test.png"); Bitmap bitmap=BitmapFactory.decodeStream(fis); 2. 以R文件的方式,假设 res/drawable下有 test.jpg文件Bitmapbitmap =BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test); 3.以ResourceStream的方式,但不用到R文件。 Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(“/res/drawable/test.png”)); 图片名就可以读取到 Bitmap啦。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; //图片宽高都为原来的二分之一,即图片为原来的四分一 //以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。

❷ android7.0怎么从uri中取出bitmap

private Bitmap getBitmapFromUri(Uri uri)
2 {
3 try
{
5 // 读取uri所在的图片
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
7 return bitmap;
8 }
9 catch (Exception e)
10 {
11 Log.e("[android]", e.getmessage());
12 Log.e("[Android]", "目录为:" + uri);
13 e.printStackTrace();
14 return null;
15 }
16 }

❸ android如何从SD卡读取图片文件转化为bitmap

SDK 中有专门取SD卡路径的静态方法
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}

不要写死路径

❹ Android如何最优化的读取一张Bitmap

全白java code int[] pix = new int[picw * pich];

for (int y = 0; y < pich; y++)
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
int r = ((pix[index] >> 16) & 0xff)|0xff;
int g = ((pix[index] >> 8) & 0xff)|0xff;
int b =( pix[index] & 0xff)|0xff;
pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;

}
bm1.setPixels(pix, 0, picw, 0, 0, picw, pich);
BitmapDrawable bmp11=new BitmapDrawable(bm1);

❺ android 怎么获取webview中的图片资源bitmap

加载html页面时,会在/data/data/应用package目录下生成database与cache两个文件夹
cache里面会保存webview中的图片文件,读的话需要bitmap解析流就可以了。

❻ android bitmap 从网络获取图片并处理问题 溢出

在 Java中,JavaVM拥有自动管理内存的功能,Java的GC能够进行垃圾回收,但是如果ImageView使用过多的Bitmap的话,经常会报OOM(内存溢出)。

造成内存溢出及解决方案:

  1. 使用BitmapFactory.decodeStream替代createBitmap方法

    原因是该方法直读取图片字节,调用JNI>>nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap。

  2. 使用压缩读取技术

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(imageSdUri, options);

final int height = options.outHeight;

final int width = options.outWidth;

options.inSampleSize = 1;

int w = 320;

int h = 480;

h = w*height/width;//计算出宽高等比率

int a = options.outWidth/ w;

int b = options.outHeight / h;

options.inSampleSize = Math.max(a, b);

options.inJustDecodeBounds = false;

Bitmap bitmap = BitmapFactory.decodeFile(imageSdUri, options);


3.及时释放Bitamp
Bitmap对象在不使用时,我们应该先调用recycle()释放内存,然后才它设置为null.虽然recycle()从源码上看,调用它应该能立即释放Bitmap的主要内存,但是测试结果显示它并没能立即释放内存。但是我它应该还是能大大的加速Bitmap的主要内存的释放。

❼ android中怎么获取一个bitmap在屏幕中位置

是要编写一个这样的功能么,android提供的api里面一般图片都是bitmap,可以通过canvas.drawbitmap的方法在view进行图片的绘制,在方法中需要进行传入一个类型为matrix的参数,设置martrix的一些参数就可以进行图片的放大的绘制,这只是显示,也就是mvc中的view层,然后通过提供的一些触摸函数的重写并实现自己需要的一些功能,如拖动,实际上就是改变bitmap绘制的位置。关于触摸的处理和图片的绘制可以在网上进行一些教程的搜索,我就不罗列了,最终就是需要通过控制函数来改变图片的绘制方式,这也许就是你所需要的功能。

❽ android,如何读取资源文件里的图片到bitmap里

  1. 方式:以R文件的方式

  2. 路径:假设 res/drawable下有 test.jpg文件

    Bitmap bitmap=BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test)

❾ android 已经知道路径怎么将路径中的图片变成Bitmap

/**
* 获取本地图片并指定高度和宽度
*/
public static Bitmap getNativeImage(String imagePath)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options); //此时返回myBitmap为空
//计算缩放比
int be = (int)(options.outHeight / (float)200);
int ys = options.outHeight % 200;//求余数
float fe = ys / (float)200;
if (fe >= 0.5)
be = be + 1;
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
options.inJustDecodeBounds = false;
myBitmap = BitmapFactory.decodeFile(imagePath, options);
return myBitmap;
}
/**
* 以最省内存的方式读取本地资源的图片 或者SDCard中的图片
* @param imagePath
* 图片在SDCard中的路径
* @return
*/
public static Bitmap getSDCardImg(String imagePath)
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
return BitmapFactory.decodeFile(imagePath, opt);
}

阅读全文

与android读取图片的bitmap相关的资料

热点内容
直播游戏签名源码 浏览:643
杭州云服务器搭建 浏览:630
mayapython中文 浏览:358
只狼加存档应该是哪个文件夹 浏览:112
程序员秃头图片 浏览:922
思科路由器命令手册 浏览:26
android获得当前activity 浏览:833
python入门迷宫 浏览:71
Python打折代码不含商品 浏览:221
把多个Word合成一个pdf 浏览:356
aes算法描述 浏览:899
新手机压缩包在哪 浏览:782
java抽奖程序源码 浏览:700
汽车压缩机又叫 浏览:95
android读取data文件 浏览:874
红旗智联app怎么跟h5车子连接 浏览:139
材料化学pdf 浏览:114
服务器机房都有什么东西 浏览:370
最近长阴短柱量能副图指标源码 浏览:647
python字符串去除后四位 浏览:167