我寫了一個相機活動,Android的指導文件說。 我保存了這張照片,然後我只想知道這張照片的寬度和高度。 但我無法使用BitmapFactory.decodeStream。 這是我的代碼,任何人都可以幫助我?Android:無法獲得相機拍攝的照片尺寸
private PictureCallback mPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//save the photo
File pictureFile = new File("/sdcard/test/test.jpg");
if(!pictureFile.exists()) {
try {
pictureFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
//get the photo's width and height
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = null;
try {
is = new FileInputStream("/sdcard/test/test.jpg");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(is != null) {
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
int picWidth = bitmap.getWidth();
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
CameraActivity.this.setResult(RESULT_OK);
CameraActivity.this.finish();
}
};
in「Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);」,我得到了一個null的位圖。 – abnormal