在SO上也有這樣的類似問題,但沒有一個符合我的具體情況。來自onPictureTaken回調的decodeByteArray數據返回null
我正在執行使用官方tutorial的攝像頭控件。
我拍攝的方法是調用mCamera.takePicture(null, null, this)
,然後從onPictureTaken
接收回調。我遇到的問題是我似乎無法將decodeByteArray
的數據傳遞到onPictureTaken
。 decodeByteArray
繼續返回null。看來字節數組的格式是無效的。
我試圖通過FileOutputStream
照原樣保存字節數組,就像在教程中一樣,然後通過decodeFile
再次讀取字節,它仍然返回null。我試圖捕獲異常但沒有被捕獲。
請注意,我可以保存照片就好了。我無法從傳遞的參數中讀取字節。我似乎無法弄清楚這一點。
這裏是我的代碼:
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream out = null;
String fileName = Photo.getDirectory(this) + "/Confirm_" + mDateFormatter.format(new Date()) + ".jpg";
try
{
File temp = new File(fileName);
out = new FileOutputStream(temp);
out.write(data);
out.close();
try
{
// This always returns NULL
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
option.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, option);
if(bitmap == null) ; // TRUE
}
catch(Exception e)
{
e.printStackTrace();
}
/*
Camera.Parameters param = mCamera.getParameters();
List<Camera.Size> sizes = param.getSupportedPictureSizes();
Bitmap bitmap = BitmapFactory.decodeFile(fileName);
for(Camera.Size s : sizes)
{
Log.i(PhotoApplication.TAG, "Size : " + s.width + " x " + s.height);
}
*/
Intent intent = new Intent();
intent.putExtra(EXTRA_FILENAME, temp.getAbsolutePath() + "/" + temp.getName());
setResult(RESULT_OK, intent);
Toast.makeText(this, "Saving as : " + temp.getAbsolutePath() + "/" + temp.getName(), Toast.LENGTH_LONG).show();
}
catch(FileNotFoundException e)
{
}
catch(IOException e)
{
}
finally
{
try
{
if(out != null)
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
finish();
}
該死的......這是對我的一記耳光。非常感謝你! –