Create a file from drawable

后端 未结 4 1287
滥情空心
滥情空心 2020-12-30 07:04

I have a large number of resources in my drawable folder.All are having big size more than 500KB. I have to load all these 25 images all at once in a srollView. As usual I r

相关标签:
4条回答
  • 2020-12-30 07:23

    You can open an InputStream from your drawable resource using following code:

    InputStream is = getResources().openRawResource(id);
    

    here id is the identifier of your drawable resource. for eg: R.drawable.abc

    Now using this input stream you can create a file. If you also need help on how create a file using this input stream then tell me.

    Update: to write data in a file:

    try
        {
        File f=new File("your file name");
        InputStream inputStream = getResources().openRawResource(id);
        OutputStream out=new FileOutputStream(f);
        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0)
        out.write(buf,0,len);
        out.close();
        inputStream.close();
        }
        catch (IOException e){}
        }
    
    0 讨论(0)
  • 2020-12-30 07:27

    I like shortcuts so I prefer using this.

    For creatting file from drawable see this.

    Put this in your build.gradle

    compile 'id.zelory:compressor:1.0.4'
    

    And wherever you want to compress the image put

     Bitmap compressedImageFile = Compressor.getDefault(context).compressToBitmap(your_file);
    

    README.md provides much more information. Sorry for bringing the answer after 6 years.

    0 讨论(0)
  • 2020-12-30 07:29

    I took cue from @mudit's answer and created the drawable from Input Stream. And later loaded the drawable to the ImageView in the Adapter.

    InputStream inputStream = mContext.getResources().openRawResource(R.drawable.your_id);

    Bitmap b = BitmapFactory.decodeStream(inputStream);
    b.setDensity(Bitmap.DENSITY_NONE);
    Drawable d = new BitmapDrawable(b);
    mImageView.setImageDrawable(d);
    

    Here is the detailed version of solution

    0 讨论(0)
  • 2020-12-30 07:35

    bro there are two methods

    1. Easiest one Use some 3rd party library
    2. Or use Bitmap class to scale drawable down

    just use Bitmap.createScaledBitmap method to compress drawables

    steps :

    // Step 1 Load drawable and convert it to bitmap

    Bitmap b = BitmapFactory.decodeResource( context , resId )

    // Step 2 reScale your Bitmap

    Bitmap nBitmap = b.createScaledBitmap( getResources() , newHieght , newWidth , true );  
    

    // Step 3 Create a drawable from bitmap

    BitmapDrawable drawable = new BitmapDrawable(nBitmap);
    

    I highly recommend you to use 3rd part library because this method is very expensive

    like https://github.com/Tourenathan-G5organisation/SiliCompressor

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