如何根据屏幕分辨率调整图像大小并将其加载到android应用(ImageView)中?

时间:2018-08-23 11:37:57

标签: android android-imageview android-image android-bitmap

我正在开发一个Wallpaper应用程序,在该应用程序中,我将通过Firebase和Picasso在GridView中显示图像。单击图像时,它将在全屏的新活动中打开。它适用于较小的图像(<200kb),但对于较大的图像,单击图像进行全屏查看时我的应用程序崩溃。

我想按照屏幕分辨率以调整大小的形式显示图像。如果一台具有720x1280的设备打开该应用,则图像会转换为720x1280。如果另一台具有1080x1920的设备打开了应用程序,则图像将转换为1080x1920。

RecyclerView ViewHolder

public void setDetails(Context ctx, String image){

        ImageView mImageTv = mView.findViewById(R.id.rImageView);

        Picasso.get().load(image).into(mImageTv);

    }

使用putExtra将图片从Fragment传递到FullscreenActivity

ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
                viewHolder.setOnclickListener(new ViewHolder.ClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        ImageView mImageView = view.findViewById(R.id.rImageView);

                        Drawable mDrawable = mImageView.getDrawable();
                        Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();

                        Intent intent = new Intent(view.getContext(), PostDetailsActivity2.class);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();

                        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] bytes = stream.toByteArray();
                        intent.putExtra("image", bytes);
                        startActivity(intent);


                    }

在“全屏活动”中显示图像

mImageTv = findViewById(R.id.full_imageView1);


        byte[] bytes = getIntent().getByteArrayExtra("image");
        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

        mImageTv.setImageBitmap(bmp);

异常StackTrace

08-23 17:16:33.982 16998-16998/? I/art: Late-enabling -Xcheck:jni
08-23 17:16:34.028 16998-16998/? D/TidaProvider: TidaProvider()
08-23 17:16:34.043 16998-16998/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch
        at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338)
        at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375)
        at miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800)
        at miui.util.ReflectionUtils.tryCallMethod(ReflectionUtils.java:818)
        at android.os.BaseLooper.enableMonitor(BaseLooper.java:47)
        at android.os.Looper.prepareMainLooper(Looper.java:111)
        at android.app.ActivityThread.main(ActivityThread.java:5595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
08-23 17:16:34.092 16998-16998/com.statusstock.wallpaperhd W/System: ClassLoader referenced unknown path: /data/app/com.statusstock.wallpaperhd-1/lib/arm64
08-23 17:16:34.224 16998-16998/com.statusstock.wallpaperhd V/FA: Registered activity lifecycle callback
08-23 17:16:34.237 16998-17019/com.statusstock.wallpaperhd W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
08-23 17:16:34.252 16998-16998/com.statusstock.wallpaperhd D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
08-23 17:16:34.254 16998-16998/com.statusstock.wallpaperhd I/FirebaseInitProvider: FirebaseApp initialization successful
08-23 17:16:34.297 16998-17021/com.statusstock.wallpaperhd W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
08-23 17:16:34.306 16998-17018/com.statusstock.wallpaperhd V/FA: App package, google app id: com.statusstock.wallpaperhd, 1:179047417165:android:c116b44eed2a7e0d
08-23 17:16:34.307 16998-17018/com.statusstock.wallpaperhd I/FA: App measurement is starting up, version: 12780
    To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
08-23 17:16:34.308 16998-17018/com.statusstock.wallpaperhd I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.statusstock.wallpaperhd
08-23 17:16:34.308 16998-17018/com.statusstock.wallpaperhd D/FA: Debug-level message logging enabled
08-23 17:16:34.315 16998-17021/com.statusstock.wallpaperhd I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
    [FirebaseAuth:] Preparing to create service connection to gms implementation
08-23 17:16:34.341 16998-17018/com.statusstock.wallpaperhd V/FA: Connecting to remote service
08-23 17:16:34.367 16998-17018/com.statusstock.wallpaperhd V/FA: Connection attempt already in progress
08-23 17:16:34.367 16998-17018/com.statusstock.wallpaperhd D/FA: Connected to remote service
08-23 17:16:34.367 16998-17018/com.statusstock.wallpaperhd V/FA: Processing queued up service tasks: 2
08-23 17:16:39.427 16998-17018/com.statusstock.wallpaperhd V/FA: Inactivity, disconnecting from the service

如果可以的话,请帮助我。...如果有人需要更多代码详细信息或有关我的问题,请对此发表评论。...谢谢

2 个答案:

答案 0 :(得分:0)

您的应用是否因为内存不足异常而崩溃?

对于处理大型位图,您应遵循以下准则:)

Loading Large Bitmaps Efficiently

答案 1 :(得分:0)

您不应传递意向额外的大对象。考虑只传递一个Uri,毕加索会从第二个活动/片段的缓存中加载它。

示例伪代码:

static class Data {
    String url;
    String name; // or whatever
}

List<Data> adapterData = null;

public void onItemClick(View view, int position) {
    Data item = adapterData.get(position);
    intent.putExtra("image", item.url);
    startActivity(intent);
}

// in your getView you're using picasso:
Picasso.with(context).load(data.url).into(imageView);

// other activity
String url = getIntent().getStringExtra("image");
Picasso.with(context).load(url).into(imageView);

毕加索应该在第二次调用中重用位图。您也可以通过使用文件/ LruCache缓存在不使用Picasso的情况下完成此操作。