Load large images into Bitmap?

前端 未结 5 2114
耶瑟儿~
耶瑟儿~ 2021-01-02 09:23

I\'m trying to make a basic application that displays an image from the camera, but I when I try to load the .jpg in from the sdcard with BitmapFactory.decodeFile

相关标签:
5条回答
  • 2021-01-02 09:45

    Well after working a lot i found out that the problem was not with code but the ram size of emulator, editing avd and increasing ram size solves all problems and saves huges pics easily. thanks.

    0 讨论(0)
  • 2021-01-02 09:47

    send:

                            BitmapFactory.Options o = new BitmapFactory.Options(); 
                        o.inJustDecodeBounds = true; 
                        BitmapFactory.decodeFile(filePath, o); 
    
                        int REQUIRED_SIZE = 640; 
                        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 bitmap = BitmapFactory.decodeFile(filePath, o2); 
    
                        ByteArrayOutputStream bs2 = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 90, bs2);
                        getIntent().putExtra("byte_picture", bs2.toByteArray());
    

    recive:

    Bitmap photo = BitmapFactory.decodeByteArray(data.getByteArrayExtra("byte_picture"),0,data.getByteArrayExtra("byte_picture").length);
    
    0 讨论(0)
  • 2021-01-02 09:57

    Here is another solution that uses inSampleSize and dynamically determines the image resolution to use based on available memory:

    http://bricolsoftconsulting.com/handling-large-images-on-android/

    0 讨论(0)
  • 2021-01-02 10:04

    Try to set the inSampleSize as shown in this example.

    0 讨论(0)
  • 2021-01-02 10:04

    You can follow this amazing tutorial

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    0 讨论(0)
提交回复
热议问题