在读取图片的时候压缩减小内存开支
2014-05-21 22:36
330 查看
这个问题真是把我折腾的够呛:
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
另外附上bitmap和SD卡的操作哦:http://blog.sina.com.cn/s/blog_a72ec20c01015rwj.html
唉,虽然今天晚上弄到现在,但明天到公司又是一场恶战。。。
package com.example.tupian; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.Menu; import android.widget.ImageView; import android.widget.Toast; @SuppressLint("NewApi") public class MainActivity extends Activity { private static Bitmap bitm; private static Bitmap bitm2; private ImageView iv; private ImageView iv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView)findViewById(R.id.iv); iv2 = (ImageView)findViewById(R.id.iv2); Thread t = new Thread(new imgThred()); t.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class imgThred implements Runnable{ public void run(){ // dd= getDrawable("http://smartcost.com.cn/global/upload/img/20140520/1400549692206.jpg"); try { bitm = revitionImageSize("http://smartcost.com.cn/global/upload/img/20140520/1400549692206.jpg",300); bitm2 = revitionImageSize("http://smartcost.com.cn/global/upload/img/20140520/1400549692206.jpg",150); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } msHandl.sendMessage(new Message()); } } Handler msHandl = new Handler(){ @Override public void handleMessage(Message msg) { iv.setImageBitmap(bitm); iv2.setImageBitmap(bitm2); } }; private Bitmap revitionImageSize(String path, int size) throws IOException { URL url; url = new URL(path); // 取得图片 InputStream temp = url.openStream(); BitmapFactory.Options options = new BitmapFactory.Options(); // 这个参数代表,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小),说白了就是为了内存优化 options.inJustDecodeBounds = true; // 通过创建图片的方式,取得options的内容(这里就是利用了java的地址传递来赋值) BitmapFactory.decodeStream(temp, null, options); // 关闭流 temp.close(); // 生成压缩的图片 int i = 0; Bitmap bitmap = null; while (true) { // 这一步是根据要设置的大小,使宽和高都能满足 if ((options.outWidth >> i <= size) && (options.outHeight >> i <= size)) { // 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流! temp = url.openStream(); // 这个参数表示 新生成的图片为原始图片的几分之一。 options.inSampleSize = (int) Math.pow(2.0D, i); // 这里之前设置为了true,所以要改为false,否则就创建不出图片 options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(temp, null, options); break; } i += 1; } double bitcount =bitmap.getByteCount()/1000; Log.e("bitcount", bitcount+""); return bitmap; } }
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
另外附上bitmap和SD卡的操作哦:http://blog.sina.com.cn/s/blog_a72ec20c01015rwj.html
唉,虽然今天晚上弄到现在,但明天到公司又是一场恶战。。。
相关文章推荐
- 在java中使用内部类进行图片压缩的时候,怎么老是报内存错误,请大侠帮忙
- 减小网络传输图片大小,使用gzip进行压缩
- [绘制相关] 用CImage 读取 内存中的图片数据
- 读取图片 优化android内存
- 在内存对图片进行操作的时候注意小数的问题
- 最新Javascript按比例压缩图片,解决onload无法读取图片的高度和宽度问题
- 利用C++函数imread读取图片的时候返回的矩阵总是空,而利用C函数cvLoadImage时却能读取到图像,疑惑
- 【Delphi】从内存读取或解压压缩文件(RAR、ZIP、TAR、GZIP等)(二)
- 【Delphi】从内存读取或解压压缩文件(RAR、ZIP、TAR、GZIP等)(一)
- Androidr的图片的缩放、读取、压缩保存
- android 最省内存的方式读取本地资源的图片
- ImageView使用和从内存读取图片显示
- Android对图片的压缩读取和保存
- 【Delphi】从内存读取或解压压缩文件(RAR、ZIP、TAR、GZIP等)(三)
- 利用GDAL读取图片,将数据以BIP的方式存在内存中。
- C#内存流示例----->用内存流来读取图片
- Android对图片的压缩读取和保存
- Android开发技术:Android对图片的压缩读取和保存
- 网页中图片src指定data url类型,达到从内存中读取图片的目的
- visual studio 2010 访问数据库的时候报错 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。