![](/img/trans.png)
[英]If I don't know if a variable is positive/negative, how do I make it positive without calling a function or using if?
[英]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?
我的相機有一個按鈕,但是我不知道如何使拍攝的照片存儲在其他地方。 你能幫我嗎? 我已經為該按鈕獲取的代碼:
<Button style="@style/ButtonsAtHome" android:onClick="cameraButton"
android:textColor="#4CAF50" android:text="CAMERA" />
Java的:
public void cameraButton(View view) {
Intent openCamera = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivity(openCamera);
getWindow().setBackgroundDrawable(null);
}
此按鈕可打開相機,但會保存在默認目錄中,但我不希望將其保存在默認目錄中,如何更改目錄或在拍攝后顯示圖像,以便可以對其進行編輯。 (我的應用是照片編輯器)
您可以使用以下代碼拍照,然后將其存儲在您的應用程序目錄中:
打開照相機
public void openCamera(View view){
Intent openCamera = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(openCamera,1);
getWindow().setBackgroundDrawable(null);
}
在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");
}
}
將圖像保存在指定的文件夾中
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();
}
}
添加必要的權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.