2012-08-27 43 views
0

我尝试使用:为什么我不能调用具有错误类型参数的方法?

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f) 
{ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_SIZE=70; 

     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

然后

R.drawable.image = decodeFile(R.drawable.image); 

如何使用这种方法吗? R.drawable.image是整数,所以它给了我错误,我怎么能解码文件?我添加240kb的图像到textview。

回答

1

,那么您已经自己说,R.drawable.image是一个整数 - 但你试图将它传递给接受File的方法,然后分配的返回值(一个Bitmap)回去吧。

此外,捕捉FileNotFoundException这样,只是吞咽它是一个非常糟糕的主意 - 为什么你不声明该方法可能会抛出异常?

目前尚不清楚你真的想要做什么 - 或者什么R.drawable.image真的是要实现 - 但为了使用你有的方法,你显然需要一个File

+0

如何从可绘制文件夹中获取文件? –

+0

@hey:我不知道,没有Android的经验 - 但我怀疑如果你可以编辑你的问题来提供更多的上下文,它会帮助别人来帮助你。 –

1

您是否试图将绘图转换为文件,然后将该文件转换为位图?

这将允许您将绘制正确解码为位图:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
+0

我将如何解码并使用decodeFile来压缩图像? –

+0

这里的想法是避免使用File,而是直接将资源解码为Bitmap。 检查这个下采样和其他选项: '位图的位图= BitmapFactory.decodeResource(getResources(),R.drawable.image,BitmapFactory.Options);' http://developer.android.com/reference/ android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources,int,android.graphics.BitmapFactory.Options) –

0

我想你可能会寻找这样的事情:

Resources res = getResources(); 
Drawable image = res.getDrawable(R.drawable.image); 

Android版允许您访问图像使用Resources类的可绘制文件夹。他们还通过为不同分辨率图像使用不同文件夹来处理缩放比例。另外,如果你有一个在xml中定义的小部件,你可以使用android:src属性为它指定一个源可绘制,在你的情况下它将是android:src="@drawable/image"。看看

http://developer.android.com/guide/topics/resources/drawable-resource.html

http://developer.android.com/training/multiscreen/screensizes.html

有关如何与您的Android处理图像和操作系统如何帮助你请分别选择适当的缩放图像,了解更多信息。

编辑:还有一件事,如果你只是想设置类似的ImageView的图像资源,你可以使用imageView.setImageResource(R.drawable.image),这样你就不必在代码中创建一个可绘制或资源对象。如果您在设置图像之前需要对图像执行某些操作,则会有相应的setImageDrawablesetImageBitmap方法。

0

VM不会让我们分配6291456个字节

你的形象是约6兆字节未经压缩的,看到http://developer.android.com/training/displaying-bitmaps/index.html

如果要加载可能因为被要求图像的缩小的版本你似乎内存不足,它将与下面的代码一起工作

/** decodes image from resources to max maxSize x maxSize pixels */ 
private Bitmap decodeFile(Context context, int resId, int maxSize) { 
    Resources res = context.getResources(); 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, o); 

    // Find the correct scale value. It should be the power of 2. 
    int scale = 1; 
    while (o.outWidth/scale/2 >= maxSize && o.outHeight/scale/2 >= maxSize) 
     scale *= 2; 

    // Decode with inSampleSize 
    o.inJustDecodeBounds = false; 
    o.inSampleSize = scale; 
    return BitmapFactory.decodeResource(res, resId, o); 
} 
+0

@zapI:其实,我的图像是240kb。 –

+0

@hey未压缩像素数据,就像将其保存为.bmp时一样? X分辨率* Y分辨率* 4,速度非常快 – zapl

相关问题