[英]I know how to make a button open the camera, but I don't how to change the location of the photos that are taken, how can I do this?
I have a button for my camera, but I can't figure out how to make the pictures taken get stored somewhere else. 我的相机有一个按钮,但是我不知道如何使拍摄的照片存储在其他地方。 Can you help? 你能帮我吗? My code that I've already got for this button: 我已经为该按钮获取的代码:
<Button style="@style/ButtonsAtHome" android:onClick="cameraButton"
android:textColor="#4CAF50" android:text="CAMERA" />
Java: Java的:
public void cameraButton(View view) {
Intent openCamera = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivity(openCamera);
getWindow().setBackgroundDrawable(null);
}
This button opens the camera, but it saves in a default directory, but I don't want it to save there, how can I change the directory, or make the image show up after I take it, so I can edit it. 此按钮可打开相机,但会保存在默认目录中,但我不希望将其保存在默认目录中,如何更改目录或在拍摄后显示图像,以便可以对其进行编辑。 (My app is a photo-editor) (我的应用是照片编辑器)
You can use below code to take picture and then store in your app directory: 您可以使用以下代码拍照,然后将其存储在您的应用程序目录中:
Open the camera 打开照相机
public void openCamera(View view){
Intent openCamera = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(openCamera,1);
getWindow().setBackgroundDrawable(null);
}
Get the result in onActivityResult() // modify it according to your own need 在onActivityResult()中获取结果//根据自己的需要对其进行修改
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
Bitmap photo = (Bitmap) data.getExtras().get("data");
createDirectoryAndSaveFile(photo,"fileName");
}
}
Save image in specified folder 将图像保存在指定的文件夹中
private void saveImageToFolder(Bitmap image, String fileName) {
File directoryName = new File(Environment.getExternalStorageDirectory() + "/MyAppDirectory");
if (!directoryName.exists()) {
directoryName.mkdir();
}
File file = new File(new File("/sdcard/MyAppDirectory/"), fileName + ".JPEG");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Add necessary permission : 添加必要的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.