无法使用以下代码在外部存储卡上写入图像
private File getOutputMediaFileToExternalMemory(int type){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(context, "SD CARD IS MOUNTED", Toast.LENGTH_SHORT).show();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Toast.makeText(context, "SD CARD IS READ ONLY", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "OTHR SD card error", Toast.LENGTH_SHORT).show();
}
String fullPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
File mediaStorageDir = new File(fullPath + "/saved_Amith_images");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
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+"Amith" + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFileToExternalMemory(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Toast.makeText(context, "Error creating media file, check storage permissions:", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Error creating media file, check storage permissions: ");
return;
}else{
Toast.makeText(context, "Save: Pic file is found going to write", Toast.LENGTH_SHORT).show();
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "Save: Pic is saved successfully", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(context, "File not found: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Toast.makeText(context, "Error accessing file: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, "Error accessing file: " + e.getMessage());
}catch (Exception e) {
Toast.makeText(context, "Other Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
写入SD卡(内部手机内存)。我想将图像写入手机中安装的外部存储卡。
获得Toast
<{1}}
我已设置权限
SD CARD IS MOUNTED
清单中的