2013-09-27 25 views
0

我搜索一种方法来有效地调整图像大小,避免“OutOfMemory”。 要做到这一点,我已经试过这个方法:http://developer.android.com/training/displaying-bitmaps/index.html 要使用这些方法,我必须有一个绘制标识,所以我创建一个这样的绘制:如何获取drawable的id?

Drawable image = Drawable.createFromPath(pathName); 

现在我不知道怎么弄可绘制的标识符。 方法getIdentifier()意味着有可绘名称,但我hav'nt。

+0

? – pskink

+0

我需要ID使用mehod:BitmapFactory.decodeResource(res,drawableID,options); – Maxime

+0

@Maxime:你不能直接通过R.drawable.yourImageName获取id吗? – epiphany27

回答

0

要使用这些方法我必须有一个可拉伸的标识符,所以我创建一个这样的可拉伸:

绘制的图像= Drawable.createFromPath(pathName中);

首先,您不需要创建drawable。其次,如果你没有在你的绘制文件夹中有一个形象则u不应使用

BitmapFactory.decodeResource(res, resId, options) 

BitmapFactory也有其他的方法,如

BitmapFactory.decodeFile (String filePath, options); //it takes your image filepath 
(in case your image is in Sdcard) as a string. 
or, 
BitmapFactory.decodeStream (InputStream, options); // if the image is placed in your asset folder instead of drawables. 
0

谢谢您的回答。 我终于选择满足这种方法来调整图片:为什么YPU需要一个ID

private void resizeBitmap(String path) { 
     Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     int sampleSize = 0; 
     boolean done = false; 
     Bitmap source = null; 
     while (!done) { 
      options.inJustDecodeBounds = false; 
      sampleSize++; 
      try { 
       options.inSampleSize = sampleSize; 
       source = BitmapFactory.decodeFile(path, options); 
       done = true; 
      } catch (OutOfMemoryError e) { 

      } 
      if (sampleSize > 20) { 
       // Exit to avoid infinite loop 
       done = true; 
      } 
     } 
     if (source != null) { 
      try { 
       FileOutputStream out = new FileOutputStream(path); 
       source.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.close(); 
      } catch (Exception e) { 
       Log.e("RESIZE PICTURE", "Unable to resize picture after download : "+e.getMessage()); 
      } 
     } 
    }