尝试从drawable调整图像大小?

时间:2015-02-13 19:53:35

标签: android android-imageview

我正在尝试调整我的drawable中包含的图像。为此,我创建了一个接收带路径图像的String的类。如果图像在存储中,它的工作正常,但如果drawable中包含的图像不起作用并抛出异常:Unable to decode stream: java.io.FileNotFoundException。路径图像为:android.resource://br.com.williarts.kontrole/2130837614

我怎么能这样做?

ImageView的

<ImageView
                android:id="@+id/ivPerfil"
                android:padding="5dp"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:scaleType="fitXY"
                />

活动

ivPerfil = (ImageView)findViewById(R.id.ivPerfil);
//image
        Uri path = Uri.parse("android.resource://" + ivPerfil.getResources().getResourcePackageName(R.drawable.icon_usuario) + "/" + R.drawable.icon_usuario);
        fotoPerfil = path.toString();
        ivPerfil.setImageURI(path);

调整图片大小

public class ResizeFileImage {
    private File file, novaImagem;
    private Integer width, height;

    public ResizeFileImage(String pathFile, Integer w, Integer h){
        Log.i("PATH FILE->", pathFile);
        file = new File(pathFile);
        this.width = w;
        this.height = h;
    }

    public File getResizeFile(){
        Bitmap myBitmap = null;

        try{            
            if(file.getPath().contains("resource")){
                //from drawable
                myBitmap = BitmapFactory.decodeFile(file.getCanonicalPath());
            }else{
                //from storage
                myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            }
        }catch(IOException e){
            e.printStackTrace();
        }


        Log.i("ORIGINAL WIDTH->", myBitmap.getWidth() + "");
        Log.i("ORIGINAL HEIGHT->", myBitmap.getHeight() + "");

        Bitmap bmp = getResizedBitmap(myBitmap, height, width);
        Log.i("CHANGE WIDTH->", bmp.getWidth() + "");
        Log.i("CHANGE HEIGHT->", bmp.getHeight() + "");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bitmapdata = bos.toByteArray();

        String nomeImg = "teste.png";
        String seuDiretorio = Environment.getExternalStorageDirectory() + File.separator + "MyApp";

        try{
            File diretorio = new File(seuDiretorio);
            if(!diretorio.exists()){
                diretorio.mkdir();
            }
            File imgToSdcard = new File(seuDiretorio + File.separator + nomeImg);
            FileOutputStream outputStream = new FileOutputStream(imgToSdcard, false);
            outputStream.write(bitmapdata , 0, bitmapdata.length);
            outputStream.flush();
            outputStream.close();
        }catch (IOException e){
            Log.e("FileOutputStream Exception Error->", e.getLocalizedMessage());
        }
        novaImagem = new File(seuDiretorio + File.separator + nomeImg);
        return novaImagem;
    }

    /** redimensiona o tamanho da imagem */
    private Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }


    public void removeNovaImagem(){
        if(novaImagem != null && novaImagem.exists()){
            novaImagem.delete();
        }
    }
}

异常

02-13 17:42:35.967  27424-27424/br.com.williarts.kontrole E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /android.resource:/br.com.williarts.kontrole/2130837614: open failed: ENOENT (No such file or directory)

1 个答案:

答案 0 :(得分:1)

您无法获取资源文件的路径,因为它们位于您的应用程序(.apk文件)中。如果您希望通过文件路径对其执行操作,则可以将资源文件复制到内部存储器。另一种解决方案是将资源加载到内存中。您可以使用以下命令获取位图:

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

或Drawable with:

Drawable d = getResources().getDrawable( R.drawable.icon );