我正在尝试制作一个应用程序,它可以捕获图片,然后将其保存在特定文件夹中。问题是我无法让文件类工作。
public class startScreen extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public Uri picUri;
public void makeFlash(View startScreen) {
startCameraActivity();
}
protected void startCameraActivity() {
// create Intent to take a picture and return control to the calling
// application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
picUri = getOutputMediaFileUri(1); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri); // set the image file
// name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_start_screen, menu);
return true;
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type) {
if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/SnapFlash", "SnapFlash");
// Create the storage directory if it does not exist
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 == 1) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "SF_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
} else{
return null;
}
}
}
在调试时,它看起来好像从未设置过 mediaStorageDir。相机确实运行,但图片保存在默认图片文件夹中。