【问题标题】:Create a file from drawable从drawable创建一个文件
【发布时间】:2011-08-19 04:48:00
【问题描述】:

我的可绘制文件夹中有大量资源。所有资源的大小都超过 500KB。我必须在 srollView 中一次加载所有这 25 张图像。像往常一样,我的内存不足。有没有办法以编程方式减小图像的大小。

我得到了这个函数,但它的参数是一个文件,我不知道如何从一个drawable创建一个文件。

私有位图解码文件(文件 f){ 位图 b = null; 尝试 { //解码图像大小 BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); 整数比例 = 1; 如果(o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE){ scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } //使用 inSampleSize 解码 BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = 比例; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } 捕捉(FileNotFoundException e){ } 返回 b; }

经过多次遍历后,我必须循环浏览此屏幕多次,系统显示内存不足,并且正在从堆栈中删除后面的其他视图,但我确实需要它。 请帮帮我。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下代码从可绘制资源中打开 InputStream:

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

    这里id 是您的可绘制资源的标识符。例如:R.drawable.abc

    现在使用这个输入流,您可以创建一个文件。如果您还需要有关如何使用此输入流创建文件的帮助,请告诉我。

    更新:将数据写入文件:

    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){}
        }
    

    【讨论】:

    • 感谢您的帮助。我收到一个错误... InputStream is = (InputStream) getResources().openRawResource(R.drawable.image); .请告诉我如何创建文件
    • 我不得不这样投。 InputStream = (InputStream) getResources().openRawResource(R.drawable.simplelist);
    • "getResources" 是在 Activity 类下定义的一个函数,所以您确定您在编写用于获取输入流的代码时拥有 Activity 的上下文。
    • 好的。您是在 IDE 中遇到错误,还是在运行项目时遇到此错误?
    • 但是文件名应该是什么,图片可能是png或jpg类型的
    【解决方案2】:

    我喜欢快捷方式,所以我更喜欢使用this。

    从drawable创建文件见this。

    把它放在你的 build.gradle

    compile 'id.zelory:compressor:1.0.4'
    

    以及你想压缩图像放在哪里

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

    README.md 提供了更多信息。很抱歉在 6 年后才给出答案。

    【讨论】:

      【解决方案3】:

      兄弟有两种方法

      1. 最简单的一个使用一些 3rd 方库
      2. 或者使用 Bitmap 类来缩小可绘制对象

      只需使用Bitmap.createScaledBitmap 方法压缩drawables

      步骤:

      // Step 1 加载drawable并将其转换为位图

      Bitmap b = BitmapFactory.decodeResource( context , resId )

      // 第 2 步重新缩放您的位图

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

      // 第三步从位图创建一个drawable

      BitmapDrawable drawable = new BitmapDrawable(nBitmap);
      

      我强烈推荐你使用第三部分库,因为这种方法非常昂贵

      点赞https://github.com/Tourenathan-G5organisation/SiliCompressor

      【讨论】:

        【解决方案4】:

        我从@mudit 的回答中得到提示,并从输入流创建了可绘制对象。然后将drawable加载到Adapter中的ImageView中。

        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是解决方案的详细版本

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-24
          • 2014-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多