使用助手类感到困惑

时间:2014-03-15 17:02:41

标签: java android

我试图使用Helper Class来获得更清晰的代码。但我现在有点困惑。让我先向您展示我的代码:

这是我的助手类代码(用于缩放位图的代码):

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);


}

这就是我想要使用的功能:

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();
                }

但我的问题是,当它在我的imageview(img_logo)中显示位图时,它不会显示照片,只显示一个空白页。

我知道问题在于最后一行助手(bmp在哪里),但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

您目前没有返回助手类加载的位图 - 您要在此行上创建它:

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

然后功能才退出,所以位图被遗忘了。您需要将其返回到调用它的函数,以便它可以显示在应用程序中:

bmp = Helper.decodeFile(filePath);

img_logo.setImageBitmap(bmp);
相关问题