简体   繁体   English

与使用辅助类混淆

[英]Confused with using a Helper Class

I'm trying to use a Helper Class for having a little cleaner code. 我正在尝试使用Helper类来获得一些更简洁的代码。 But I'm a little confused right now. 但是我现在有点困惑。 let me first show you my codes: 让我先向您展示我的代码:

this is my Helper Class Code (a code for scaling a bitmap): 这是我的助手类代码(用于缩放位图的代码):

public class Helper {
 public static 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;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    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;
    Bitmap bmp = BitmapFactory.decodeFile(filePath, o2);


}

and this is where I want to use the function: 这是我要使用功能的地方:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


               Helper.decodeFile(filePath);

               img_logo.setImageBitmap(bmp);

               settings = getSharedPreferences("pref", 0);
               Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }

but my problem is when It's going to display bitmap in my imageview(img_logo), it doesn't show the photo and just showes a blank page. 但是我的问题是,当它将在我的imageview(img_logo)中显示位图时,它不显示照片,而仅显示空白页。

I know the problem is with last line of helper (where makes the bmp) but I don't know what to do. 我知道问题出在帮助程序的最后一行(在哪里制作bmp),但我不知道该怎么办。

You're not currently returning the bitmap which your helper class loads - you're creating it on this line: 您当前未返回帮助程序类加载的位图-您正在此行上创建位图:

Bitmap bmp = BitmapFactory.decodeFile(filePath, o2);

But then the function just exits, so the bitmap's forgotten. 但是随后该函数退出,因此位图被遗忘了。 You need to return it to the function which calls it so it can then be displayed in the application: 您需要将其返回给调用它的函数,以便随后可以在应用程序中显示它:

bmp = Helper.decodeFile(filePath);

img_logo.setImageBitmap(bmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM