bitmap-如何调整gallery中的图像大小?

问题描述

如何调整gallery中的图像大小?

第一个文件允许用户从gallery中选择一个图片,然后把图片传到一个activity中处理。问题是图像相对于手机屏幕来说太大了,所以只能看见图像的左上角,就像图像是被放大的。

Button useGallery = (Button)findViewById(R.id.loadfromgallery);
        useGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            }}) ;

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                Intent intent = new Intent(mContext, DisplayUndistortedBitmapFromGalleryActivity.class);
                intent.setData(selectedImage);
                startActivity(intent);

                if(yourSelectedImage != null){
                Log.e(TAG,"pic ok");
                }else{
                     Log.e(TAG,"pic not ok");
                }
            }
        }

第二个文件是activity,从intent中接收到图片图像数据,然后把它放置在一个包含bitmap的URI中。

public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {

    private static final String TAG = "*********DUBFGActivity";
    private Context mContext = this;
    Uri uri;
    private Bitmap mbitmap = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

         uri = getIntent().getData();
        if(uri != null){
            Log.e(TAG, "uri ok");
        }else {
            Log.e(TAG, "uri not ok");
        }

        try {
              mbitmap = Media.getBitmap(getContentResolver(), uri);
             //setMbitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

第三个文件是activity的customview。下面这一行从activity中检索出bitmap并显示它。是否有精简bitmap的方法,让它符合屏幕的大小?

Bitmap bm = ((DisplayUndistortedBitmapFromGalleryActivity)getContext()).getMbitmap();

解决方案

我用过的一个最简单的方法:

public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    LinearLayout linLayout = new LinearLayout(this);

    // load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                      width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable
时间: 2024-09-06 11:06:48

bitmap-如何调整gallery中的图像大小?的相关文章

如何调整JLabel中Icon的大小

问题描述 如何调整JLabel中Icon的大小当JLabel大小改变时,里面的Icon也能随之改变大小在线等~~~~~~

《树莓派开发实战(第2版)》——1.9 调整显示器中的图像尺寸

1.9 调整显示器中的图像尺寸 面临问题当你第一次将树莓派连接到显示器上面时,可能发现某些文本无法阅读,因为它们跑到屏幕外边去了,或者遇到窗口无法充满整个屏幕的问题. 解决方案如果遇到文本延伸到屏幕之外的问题,你可以通过raspi-config工具关闭overscan功能来加以解决. 为此,可以打开一个终端会话,然后通过下列命令来运行raspi-config工具. $ sudo raspi-config``` 然后,使用光标键滚动到Advanced选项中的Overscan处,并将其关闭(见图1-

PS CC中图像大小工具详解

  今天的这篇文章讲的是大家经常用到的图像大小工具,PS CC中对这个工具进行了改进和更新,让我们在处理图片大小的时候,能够更好的保持原图的真实度.让我们一起来看下吧. PS的核心是处理图像(通常是位图).如果你能够自由操纵你创建的位图,将会收到意想不到的效果. 在改进之前,如果你的图片比较小,在处理图像大小时就会遇到困难.你可以放大图像(重新采样),但是图像的像素只有那么多,放大图像会使它变得模糊失真,这样就降低了图片的品质.处理这个问题的方法之一是使用滤镜>锐化,可以让失真后的图像恢复一点清

android-当从gallery中加载图像时,有空指针异常

问题描述 当从gallery中加载图像时,有空指针异常 我用下面的代码从 Gallery中获取图片.给出一个空指针异常并且系统崩溃.我在设备上测试了代码,只要我在gallery中选择一个图像时,系统就崩溃了.哪些地方出错了呢? AlertDialog.Builder builder = new AlertDialog.Builder(CreatePod.this); builder.setMessage("Select") .setCancelable(false).setPositi

jquery.mobile中的data-icon大小能调整吗? 求 大神解答 急

问题描述 jquery.mobile中的data-icon大小能调整吗? 求 大神解答 急 如题 急 !!! 附求方法 急急急 大神求解

java-使用文件路径在gallery中打开一个图像

问题描述 使用文件路径在gallery中打开一个图像 我在数据库中存储一个图像文件路径.现在使用它我想打开 gallery 中 SD Card 的图像. 我使用的下面的代码: File file = new File(filename); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.setType("image/*"); startActivity(i

单目视觉定位中根据图像中目标物大小确定距离,有没有什么可用的算法?

问题描述 单目视觉定位中根据图像中目标物大小确定距离,有没有什么可用的算法? 在做机器人单目视觉定位 现在可以测出目标物对机器人的角度 让机器人朝目标物走 如何知道机器人到什么位置停下来 解决方案 http://wenku.baidu.com/link?url=AV4BF8YhP7MhWfLFtZWt_-JRM80tSZqB__nSbo56pfWYZHZT7VSfA2_qYHZ-IBgVuezA0-n4W9tRpPzK1CYlfXe9OJ1vZ5VVm-GF5Bo_UfKhttp://wenku

在ASP.NET 中为图像加入版权信息

asp.net 你试过用.net来进行图像处理吗?公司最近进行的一个项目,数据库中存入了很多的产品图片,可是都没有版权信息,当时客户要求将所有的图片打上公司名称?这时候,你该怎么办? 虽说我还是江湖嫩手,可是对这一问题的解决方案还是很多,归结如下: 1.使用图形处理软件,如Photoshop等,利用其批处理功能,可以实现这个功能,可是每次数据录入员加进图片的话,还得加工一下图片,很麻烦的,看录入员大姐平常对我很不错,每日笑脸相迎,能忍心折磨她么?这个方案否决了. 2.使用.net行云流水般的图像

android中圆角图像生成方法

  这篇文章主要介绍了android中圆角图像生成方法,涉及Android处理圆角图像的技巧,需要的朋友可以参考下 本文实例讲述了android中圆角图像生成方法.分享给大家供大家参考.具体分析如下: 在android开发中为了美观,常常要求ImageView中显示出圆角图像的效果,这个如何实现? 这里总结了网上的最优方法:将图像处理成圆角,然后在加载给ImageView显示,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public Bitmap