在android中更改图像文件资源
我使用以下代码将捕获的图像保存在SD卡中在android中更改图像文件资源,android,file,Android,File,我使用以下代码将捕获的图像保存在SD卡中 class SavePhotoTask extends AsyncTask<byte[], String, String> { @Override protected String doInBackground(byte[]... jpeg) { File photo=new File(Environment.getExternalStorageDirectory(),"photo.jpg"); if (photo.exists()) { p
class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo=new File(Environment.getExternalStorageDirectory(),"photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
return(null);
}
}
提前感谢传递给
doInBackground
方法的jpeg已具有该分辨率-您需要更改调用此代码的内容。如果您可以将其解析为位图,则可以使用以下方法:
private final int MAX_WIDTH = 400;
private final int MAX_HEIGHT = 400;
public Bitmap getResizedBitmap(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth;
float scaleHeight;
if (width < MAX_WIDTH && height < MAX_HEIGHT) {
return bm;
}
if (width > height) {
scaleWidth = ((float) MAX_WIDTH) / width;
scaleHeight = ((float) MAX_HEIGHT * height / width) / height;
} else {
scaleWidth = ((float) MAX_WIDTH * width / height) / width;
scaleHeight = ((float) MAX_HEIGHT) / height;
}
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
private final int MAX_WIDTH=400;
私人最终内部最大高度=400;
公共位图getResizedBitmap(位图bm){
int width=bm.getWidth();
int height=bm.getHeight();
浮标宽度;
浮标重量;
if(宽度<最大宽度和高度<最大高度){
返回bm;
}
如果(宽度>高度){
缩放宽度=((浮动)最大宽度)/宽度;
缩放高度=((浮动)最大高度*高度/宽度)/高度;
}否则{
缩放宽度=((浮动)最大宽度*宽度/高度)/宽度;
缩放高度=((浮动)最大高度)/高度;
}
矩阵=新矩阵();
矩阵。后标度(标度宽度、标度高度);
Bitmap resizedBitmap=Bitmap.createBitmap(bm,0,0,宽度,高度,
矩阵,假);
返回resizedBitmap;
}
否,更改发生在您拥有的其他代码中,此处打印的内容与此无关。很抱歉,这需要一段时间-在创建摄影机对象后,您似乎需要设置选项,不幸的是,我不知道这些选项是什么,但这应该可以让您开始。
private final int MAX_WIDTH = 400;
private final int MAX_HEIGHT = 400;
public Bitmap getResizedBitmap(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth;
float scaleHeight;
if (width < MAX_WIDTH && height < MAX_HEIGHT) {
return bm;
}
if (width > height) {
scaleWidth = ((float) MAX_WIDTH) / width;
scaleHeight = ((float) MAX_HEIGHT * height / width) / height;
} else {
scaleWidth = ((float) MAX_WIDTH * width / height) / width;
scaleHeight = ((float) MAX_HEIGHT) / height;
}
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}