Android 调用onPictureTaken中的位图
如何在onPictureTaken方法中调用位图?。除非我重新启动手机,否则图像不会存储在多媒体资料中 请帮助重新安排代码Android 调用onPictureTaken中的位图,android,android-layout,android-camera,Android,Android Layout,Android Camera,如何在onPictureTaken方法中调用位图?。除非我重新启动手机,否则图像不会存储在多媒体资料中 请帮助重新安排代码 public static int getOrientation(Context context,Uri photoUri){ Cursor cursor = context.getContentResolver().query(photoUri,new String[] {MediaStore.Images.ImageColumns.ORIENTATION},nu
public static int getOrientation(Context context,Uri photoUri){
Cursor cursor = context.getContentResolver().query(photoUri,new String[] {MediaStore.Images.ImageColumns.ORIENTATION},null,null,null);
if(cursor.getCount()!=1){
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
从未使用getCorrectlyOrientatedImage()。请帮助我在哪里可以调用此函数
public static Bitmap getCorrectlyOrientatedImage(Context context, Uri photoUri)throws IOException{
InputStream is=context.getContentResolver().openInputStream(photoUri);
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,dbo);
is.close();
int rotatedWidth,rotatedHeight;
int orientation = getOrientation(context,photoUri);
if(orientation == 90 || orientation ==270){
rotatedWidth=dbo.outHeight;
rotatedHeight=dbo.outWidth;
}else{
rotatedWidth=dbo.outWidth;
rotatedHeight=dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if(rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION ){
float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
float maxRatio = Math.max(widthRatio,heightRatio);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
}else{
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
if(orientation > 0){
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap,0,0,srcBitmap.getWidth(),srcBitmap.getHeight(),matrix,true);
}
return srcBitmap;
}
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.d("onPictureTaken", "File is assigned to pictureFile");
if (pictureFile == null) {
Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
return;
}
try {
Bitmap bitmap=getCorrectlyOrientatedImage()
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
} catch (FileNotFoundException e) {
Log.d("DG_DEBUG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
}
}
};
在我重新启动手机之前,图像不会存储在SD卡中,而且存储的图像存储方向错误。若照片是纵向拍摄的,则图像存储在横向中。如何以正确的方向存储图像
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"SELFie");
if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdir()){
Log.d(DEBUG_TAG,"Filed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
我知道这个问题有点老了,但为了防止有人面临同样的问题,解决方案是将图像保存在背景线程中,如中所示
private PictureCallback mPicture=new PictureCallback(){
公共void onPictureTaken(字节[]数据,摄像头){
新建SaveImageTask().execute(数据);
}
};
私有类SaveImageTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(字节[]…数据){
File pictureFile=getOutputMediaFile(媒体类型图像);
Log.d(“onPictureTaken”,“文件被分配给pictureFile”);
如果(pictureFile==null){
d(DEBUG_标记,“创建媒体文件时出错,请检查存储权限:”);
返回null;
}
试一试{
位图位图=getCorrectlyOrientatedImage()
FileOutputStream fos=新的FileOutputStream(pictureFile);
写入(数据);
fos.close();
File mediaStorageDir=新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),“SELFie”);
String path=mediaStorageDir.getPath()+File.separator+“IMG\u Some\u name.jpg”;
SCamera.this.sendBroadcast(新的Intent(Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u文件,Uri.parse(“文件:/”+path));
}catch(filenotfounde异常){
Log.d(“DG_调试”,“未找到文件:+e.getMessage());
}捕获(IOE异常){
Log.d(“DG_调试”,“访问文件时出错:”+e.getMessage());
}
}
}
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask().execute(data);
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.d("onPictureTaken", "File is assigned to pictureFile");
if (pictureFile == null) {
Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
return null;
}
try {
Bitmap bitmap=getCorrectlyOrientatedImage()
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
} catch (FileNotFoundException e) {
Log.d("DG_DEBUG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
}
}
}