简体   繁体   English

我知道如何打开相机的按钮,但是我不知道如何更改拍摄照片的位置,我该怎么做?

[英]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.

相关问题 如果我不知道变量是正/负,如何在不调用函数或不使用if的情况下使其变为正数? - 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 don´t know how to change my program so that i can see the ten largest files 如何在Android设备上访问相机胶卷/照片? - how can i access the camera roll/Photos on Android? 我不知道如何解决此NullPointerException - I don't know how to fix this NullPointerException 我不知道如何使用 JPQL [AND] 和 [OR] - I don't know how to use JPQL [AND] and [OR] 如果我不知道我将传入多少参数,如何使用预准备语句? - How do I use a prepared statement if I don't know how many parameters I'll be passing in? 在我点击 JOptionPane 上的确定按钮后,我不知道如何重置第二帧中的所有 function。 该怎么办? - I don't know how to reset all the function in the 2nd frame after i hit the okay button on the JOptionPane. What to do? 我正在学习如何为控制器进行单元测试,但我不知道如何正确地进行 - I am learning how to do the unit tests for the controllers and i don't know how to do it correctly 我正在尝试制作基于控制台的战舰游戏,但我不知道如何设置船只 - I'm trying to make a console based battleship game but I don't know how to set the ships 我使用 Glide 库将图像加载到 imageView 中,但我不知道如何将图像缩放为可缩放 - I used Glide library to load image into imageView and I don't know how to make image pinch to zoomable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM