android-imageview - 尝试从图形中调整图像大小?

  显示原文与译文双语对照的内容
0 0

我正在尝试调整图像中包含的图像大小。 为此,我创建了一个接收带有路径图像的字符串的类。 如果图像位于存储区中,但如果包含在可以绘图中的图像不起作用,则引发异常: Unable to decode stream: java.io.FileNotFoundException 路径图像为: android.resource://br.com.williarts.kontrole/2130837614

我怎么能做到?

英镑


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

Activity


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)

时间: 原作者:

0 0

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


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

或者可以使用:


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

...