gpt4 book ai didi

android - 尝试调整可绘制图像的大小?

转载 作者:行者123 更新时间:2023-11-30 02:21:21 24 4
gpt4 key购买 nike

我正在尝试调整我的可绘制对象中包含的图像的大小。为此,我创建了一个接收带有路径图像的字符串的类。如果图像在存储中,则它工作正常,但如果可绘制对象中包含的图像不起作用并引发异常: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"
/>

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)

最佳答案

您无法获取资源文件的路径,因为它们位于您的应用程序(.apk 文件)中。如果您想对其进行操作,可以将资源文件复制到内部存储器中,这要归功于文件的路径。另一种解决方案是将资源加载到内存中。你可以得到一个位图:

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

或一个可绘制的:

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

关于android - 尝试调整可绘制图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28507685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com