我正在尝试在Android应用程序中保存图片。这是我的回调函数:
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
Log.d(TAG, "shutter callback");
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Log.d(TAG, "raw callback");
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.BUCKET_ID, "test");
values.put(Images.Media.DESCRIPTION, "test Image taken");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(CameraView.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.d(TAG, "file not found " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "io exception " + e.getMessage());
e.printStackTrace();
}
//camera.startPreview();
finish();
}
};
问题是,对于几种不同类型的手机,我在此行获得了NullPointerException
:
imageFileOS = getContentResolver().openOutputStream(uriTarget);
有没有人遇到过这个?
04-24 11:52:03.979: E/AndroidRuntime(12695): FATAL EXCEPTION: main
04-24 11:52:03.979: E/AndroidRuntime(12695): java.lang.NullPointerException
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:465)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.content.ContentResolver.openOutputStream(ContentResolver.java:361)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.content.ContentResolver.openOutputStream(ContentResolver.java:337)
04-24 11:52:03.979: E/AndroidRuntime(12695): at com.appfury.grafighters.CameraView$4.onPictureTaken(CameraView.java:234)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:536)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.os.Looper.loop(Looper.java:150)
04-24 11:52:03.979: E/AndroidRuntime(12695): at android.app.ActivityThread.main(ActivityThread.java:4263)
04-24 11:52:03.979: E/AndroidRuntime(12695): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 11:52:03.979: E/AndroidRuntime(12695): at java.lang.reflect.Method.invoke(Method.java:507)
04-24 11:52:03.979: E/AndroidRuntime(12695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 11:52:03.979: E/AndroidRuntime(12695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 11:52:03.979: E/AndroidRuntime(12695): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
它与this问题类似。如果媒体提供程序抛出任何异常,请检查logcat,并在调用时使用调试器检查uriTarget
是否为null
imageFileOS = getContentResolver().openOutputStream(uriTarget);
答案 1 :(得分:0)
事实证明问题是HTC设备特有的。我通过将图像文件保存在设备上的DCIM文件夹中解决了问题,但没有使用getContentResolver来获取路径。