Prevent bitmap too large to be uploaded into a texture android

后端 未结 6 1905
走了就别回头了
走了就别回头了 2021-01-17 10:02

I need to display original image in full screen in gallery form. For thumb it will be work perfectly and when I try to display that image in full screen with original source

相关标签:
6条回答
  • 2021-01-17 10:17

    Google provided a training how to do that. Download the sample from Displaying Bitmaps Efficiently

    Take a look to ImageResizer class. ImageResizer.decodeSampledBitmapFrom* use this method to get downscaled image.

    0 讨论(0)
  • 2021-01-17 10:19

    I found a way to do this without using any external libraries:

    if (bitmap.getHeight() > GL10.GL_MAX_TEXTURE_SIZE) {
    
        // this is the case when the bitmap fails to load
        float aspect_ratio = ((float)bitmap.getHeight())/((float)bitmap.getWidth());
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        (int) ((GL10.GL_MAX_TEXTURE_SIZE*0.9)*aspect_ratio),
                        (int) (GL10.GL_MAX_TEXTURE_SIZE*0.9));
        imageView.setImageBitmap(scaledBitmap);
    }
    else{
    
        // for bitmaps with dimensions that lie within the limits, load the image normally
        if (Build.VERSION.SDK_INT >= 16) {
            BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
            imageView.setBackground(ob);
        } else {
            imageView.setImageBitmap(bitmap);
        }
    }
    

    Basically, the maximum image dimensions are a system-imposed limit. The above approach will correctly resize bitmaps that exceed this limit. However, only a portion of the entire image will be loaded. To change the region displayed, you can alter the x and y parameters of the createBitmap() method.

    This approach handles bitmaps of any size, including pictures taken with professional cameras.

    References:

    Android Universal Image Loader.

    0 讨论(0)
  • 2021-01-17 10:21

    I came across the same problem and came up with a one liner solution for this problem here:

    Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);
    
    0 讨论(0)
  • 2021-01-17 10:31

    This is the code I used to rectify my problem of fitting an image of size 3120x4196 resolution in an image view of 4096x4096 resolution. Here ImageViewId is the id of the image view created in the main layout and ImageFileLocation is the path of the image which is to be resized.

            ImageView imageView=(ImageView)findViewById(R.id.ImageViewId);          
            Bitmap d=BitmapFactory.decodeFile(ImageFileLcation);
            int newHeight = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
            Bitmap putImage = Bitmap.createScaledBitmap(d, 512, newHeight, true);
            imageView.setImageBitmap(putImage);
    
    0 讨论(0)
  • 2021-01-17 10:41

    You don't need to load the whole image, cause it's too large and probably your phone won't able to show the full bitmap pixels. You need to scale it first according to your device screen size. This is the best method that I found and it works pretty good: Android: Resize a large bitmap file to scaled output file

    0 讨论(0)
  • 2021-01-17 10:43

    i just created a if else function to check if the image is bigger than 1M pixels here's the sample code:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
       if (resultCode == RESULT_OK) {
    
         if (requestCode == SELECT_PICTURE) {
    
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
    
            Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
            int height = bitmap.getHeight(), width = bitmap.getWidth();
    
            if (height > 1280 && width > 960){
                Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
                imageView.setImageBitmap(imgbitmap);
    
                System.out.println("Need to resize");
    
            }else {
                imageView.setImageBitmap(bitmap);
                System.out.println("WORKS");
            }
    
    0 讨论(0)
提交回复
热议问题