无法将相机拍摄的照片保存到 SD 卡上
Unable to save Photos taken by camera on SD Card
我有一个使用相机 API 的应用程序,它会在通话时拍照
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
mCamera.takePicture(shutterCallback, rawCallback, mPicture);
mCamera.startPreview();
m图片定义为
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: " /* +
e.getMessage()*/);
Toast.makeText(MainActivity.this, "Failed to write Photo to File", Toast.LENGTH_SHORT);
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
Toast.makeText(MainActivity.this, "Writing Photo to File", Toast.LENGTH_SHORT);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
public static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
mediaFile = new File(dir, "IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
为什么没有保存图片?我去我的画廊和文件管理器应用程序检查,没有。
我还注意到当我退出应用程序时我的应用程序崩溃了,这可能是原因吗?
我的onPause方法如下
@Override
protected void onPause() {
mSensorManager.unregisterListener(mShakeDetector);
letGo();
super.onPause();
}
private void letGo(){
if(mCamera != null){
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
cameraPreview.getHolder().removeCallback(cameraPreview);
mCamera.release();
mCamera = null;
}
}
Why isn't any pictures saved?
他们完全有可能得救。但是,请记住,您正在主应用程序线程上执行 disk I/O;根据 StrictMode
设置,您可能会因此 I/O.
而崩溃
如果图片未保存,您应该会在 LogCat 中看到您的日志消息。不过,我建议将它们切换到错误严重性 (Log.e()
)。
I went to my gallery & file manager app to check, nothing.
在相当长的一段时间内,它们不会出现在设备 "gallery" 风格的应用程序中,也不会出现在您的桌面 OS 的文件管理器中。那是因为那些工具使用 MediaStore
来查看存在的内容,而您还没有安排让 MediaStore
为您的文件编制索引。为此,请使用 MediaScannerConnection
及其 scanFile()
方法。
I also noticed that my app crashes when I exit the app
Use LogCat to examine the Java stack trace associated with your crash.
我有一个使用相机 API 的应用程序,它会在通话时拍照
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
mCamera.takePicture(shutterCallback, rawCallback, mPicture);
mCamera.startPreview();
m图片定义为
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: " /* +
e.getMessage()*/);
Toast.makeText(MainActivity.this, "Failed to write Photo to File", Toast.LENGTH_SHORT);
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
Toast.makeText(MainActivity.this, "Writing Photo to File", Toast.LENGTH_SHORT);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
public static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
mediaFile = new File(dir, "IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
为什么没有保存图片?我去我的画廊和文件管理器应用程序检查,没有。 我还注意到当我退出应用程序时我的应用程序崩溃了,这可能是原因吗?
我的onPause方法如下
@Override
protected void onPause() {
mSensorManager.unregisterListener(mShakeDetector);
letGo();
super.onPause();
}
private void letGo(){
if(mCamera != null){
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
cameraPreview.getHolder().removeCallback(cameraPreview);
mCamera.release();
mCamera = null;
}
}
Why isn't any pictures saved?
他们完全有可能得救。但是,请记住,您正在主应用程序线程上执行 disk I/O;根据 StrictMode
设置,您可能会因此 I/O.
如果图片未保存,您应该会在 LogCat 中看到您的日志消息。不过,我建议将它们切换到错误严重性 (Log.e()
)。
I went to my gallery & file manager app to check, nothing.
在相当长的一段时间内,它们不会出现在设备 "gallery" 风格的应用程序中,也不会出现在您的桌面 OS 的文件管理器中。那是因为那些工具使用 MediaStore
来查看存在的内容,而您还没有安排让 MediaStore
为您的文件编制索引。为此,请使用 MediaScannerConnection
及其 scanFile()
方法。
I also noticed that my app crashes when I exit the app
Use LogCat to examine the Java stack trace associated with your crash.