嗨,我是Android的新人....我想在我拍摄图像时裁剪并保存sd卡中的图像....当我们拍摄图像时,它不会存储在SD卡中...之后裁剪只保存在SD卡?...图像质量不应该影响...我们可以知道这条路径..我没有关于此的代码...请提供完整的代码,如果你可以..这代码是强制关闭..我不知道如何解决这个问题.. 请事先告诉我谢谢
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class ImageResize {
private Context mContext;
private int mWidth;
private int mHeight;
private Uri mImageUri;
private BitmapFactory.Options mBitMapOptions;
private Bitmap mBitMap;
private Bitmap tempBitMap;
public ImageResize(Context context, int width, int height, Uri imgUri){
this.mContext = context;
this.mWidth = width;
this.mHeight = height;
this.mImageUri = imgUri;
}
public Bitmap getResizeImage(){
ContentResolver resolver = mContext.getContentResolver();
mBitMapOptions = new BitmapFactory.Options();
if(mImageUri != null){
ParcelFileDescriptor fd = null;
try {
fd = resolver.openFileDescriptor(mImageUri, "r");
int sampleSize = 1;
mBitMapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);
int nextWidth = mBitMapOptions.outWidth >> 1;
int nextHeight = mBitMapOptions.outHeight >> 1;
while(nextWidth > mWidth && nextHeight > mHeight){
sampleSize <<= 1;
nextWidth >>= 1;
nextHeight >>= 1;
}
mBitMapOptions.inSampleSize = sampleSize;
mBitMapOptions.inJustDecodeBounds = false;
mBitMap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);
Log.d("Result","Image use Size : " +mWidth+"," +mHeight);
Log.d("Result","Image Size : " +mBitMap.getWidth()+"," + mBitMap.getHeight());
Log.d("Result","aa : " +(mWidth*mBitMap.getHeight())/mBitMap.getWidth());
if(mBitMap!=null){
if(mBitMapOptions.outWidth != mWidth || mBitMapOptions.outHeight != mHeight){
//??????? : ???? ???????? = (???? ???????? * ?????????) / ?????????
tempBitMap = Bitmap.createScaledBitmap(mBitMap, mWidth, (mWidth*mBitMap.getHeight())/mBitMap.getWidth(), true);
mBitMap.recycle();
mBitMap = tempBitMap;
}
}
return mBitMap;
} catch (FileNotFoundException e) {
Log.e(getClass().getSimpleName(), e.getMessage(), e);
} finally {
try { if(fd != null) fd.close(); } catch (IOException e) { Log.e(getClass().getSimpleName(), e.getMessage(), e);}
if(mBitMap != null) mBitMap = null;
if(tempBitMap != null) tempBitMap = null;
}
}
return null;
}
}
答案 0 :(得分:0)
如果您已经有位图,则以下代码将裁剪它并将其保存在指定路径(确保路径存在)。该文件将以指定的格式保存。如果选择jpeg,则必须指定品质因数。保存在png中会忽略质量参数。
File outputFile = new File("/sdcard/SaveDir/SaveBitmap.png");
//x,y is the starting point and width,height is the distance from start
//createBitmap(Bitmap source, int x, int y, int width, int height)
Bitmap bitmap = Bitmap.createBitmap(originalBitmap, 0, 0, 20, 20);
OutputStream fout = null;
try {
fout = new FileOutputStream(outputFile);
//if format is png(lossless) then quality argument is ignored
//(Bitmap.CompressFormat format, int quality, OutputStream stream)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}