无法在Android中写入外部SD卡
有了这段代码,我就可以存储到设备存储器中 设备存储/模拟/0/camtest 我想把它存储到外部SD卡上。我该怎么做 我有以下许可无法在Android中写入外部SD卡,android,file-io,storage,android-sdcard,Android,File Io,Storage,Android Sdcard,有了这段代码,我就可以存储到设备存储器中 设备存储/模拟/0/camtest 我想把它存储到外部SD卡上。我该怎么做 我有以下许可 PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { FileOutputStream outStream = null; try {
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// Write to SD Card
fileName = String.format("%d.jpg", System.currentTimeMillis());
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/camtest");
if(!file.exists())
file.mkdirs();
File outputFile = new File(file, fileName);
outStream = new FileOutputStream(outputFile);
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
resetCam();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
这是我在一个类中拥有的代码,我用来拍照并将其保存到SD卡上的文件夹中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
测试时确保设备未通过USB根目录。嗨@Hank否我的设备未根目录它是否记录了wroteBytes行?是@superfell它确实记录了wroteBytes行,大小为well@superfell我还可以访问文件管理器中写入的文件,您的代码仍保存在手机目录中。我也尝试重新启动我的设备---@Hank@Hank--我已经取消了祝酒词的注释,以便看到祝酒词上写着“我们可以读和写”。拍完照片后,它从应用程序中退出了。哦,可能是因为我已经完成了,它在我脸上退出了
makeOutPutFolder();
Button capture = (Button) findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(DEBUG_TAG, "Requesting capture");
cameraView.capture(new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.v("Still", "Image data received from camera");
try {
File exportDir = new File(Environment.getExternalStorageDirectory(), "student_images");
if (!exportDir.exists())
{
exportDir.mkdirs();
}
Log.d("Still image filename:", "capture.jpg");
File file = new File(exportDir + File.separator + STILL_IMAGE_FILE);
FileOutputStream WriteStudentImage = new FileOutputStream(file);
WriteStudentImage.write(data);
WriteStudentImage.close();
Toast.makeText(getApplicationContext(), "Finished Saving Image", Toast.LENGTH_LONG).show();
//updateImageCount(currentAnimal);
finish();
} catch (Exception e) {
Log.e("Still", "Error writing file", e);
}
}
});
}
});
}
public void makeOutPutFolder(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)){
//We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
// Toast.makeText(getApplicationContext(), "We Can Read And Write ", Toast.LENGTH_LONG).show();
File file = new File(Environment.getExternalStorageDirectory()
+File.separator
+"student_images"); //folder name
file.mkdir();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
// Toast.makeText(getApplicationContext(), "We Can Read but Not Write ", Toast.LENGTH_LONG).show();
}else{
//something else is wrong
mExternalStorageAvailable = mExternalStorageWriteable = false;
// Toast.makeText(getApplicationContext(), "We Can't Read OR Write ", Toast.LENGTH_LONG).show();
}
}