调整图像大小时的字符串问题

时间:2014-01-02 10:26:58

标签: android bitmap imageview

我使用下面的代码将图片从我的图库导入到我的imageView上。它是成功的,但我想调整位图的大小,因为app force使用大量图像导入来关闭它。

String mPicPath1;
Button save;
ImageView logoview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);

mPicPath1 = null;

save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {

@Override   
public void onClick(View v) {

Intent intent = new Intent(NewCard.this, Template.class);
 if (!TextUtils.isEmpty(mPicPath1)) {
intent.putExtra("picture_path1", mPicPath1);
}

startActivity(intent);

}}
});

logoview=(ImageView)findViewById(R.id.logoview);
    logoview.setScaleType(ScaleType.FIT_XY);
    logoview.setOnClickListener(new OnClickListener() 
    {            

        @Override
        public void onClick(View arg0) {

            openGallerylogo();
        }   
    }); 

private void openGallerylogo() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
super.onActivityResult(requestCode, resultcode, data);
if (requestCode == 1) 
    {
        if (data != null && resultcode == RESULT_OK) 
        {              

            Uri selectedImage = data.getData();

            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mPicPath1 = cursor.getString(columnIndex);
            cursor.close();

logoview.setBackgroundResource(0);
logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));

}}  
  }
   }

当我实施时:

mPicPath1.setScaleType(ScaleType.FIT_XY);

然后我需要将mPicPath1从String更改为Bitmap。如果我改变那么我有“BitmapFactory类型中的方法decodeFile(String)不适用于参数”这一行中的错误:

logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));

请您更正我上面的代码以导入大尺寸图片并将其传递给SecondActivity。谢谢。

2 个答案:

答案 0 :(得分:1)

你应该用这个:

public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;

    int scale = 3;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);


}

并将此功能称为:

      decodeFile(mPicPath1 );

<强>更新 您可以在代码中使用like: 制作全局位图变量

  Bitmap  bmp ;

  decodeFile(mPicPath1);
  logoview.setImageBitmap(bmp);

答案 1 :(得分:0)

只需完成此操作即可管理大型位图: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html