在Android中以编程方式拍照

时间:2014-05-15 05:19:27

标签: android image android-camera

我正在开发一款使用相机的Android应用程序......我的应用程序的要求是:

我想拍摄照片而不会打扰前景应用程序,并将此图像存储在设备存储上 png 格式。

2 个答案:

答案 0 :(得分:0)

HI使用以下代码:

在按钮内添加以下代码Click Event:

    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File folder = new File(Environment.getExternalStorageDirectory() + "/"
            + getResources().getString(R.string.app_name));

    if (!folder.exists()) {
        folder.mkdir();
    }
    final Calendar c = Calendar.getInstance();
    String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
            + ((c.get(Calendar.MONTH)) + 1) + "-" + c.get(Calendar.YEAR)
            + " " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)
            + "-" + c.get(Calendar.SECOND);
    Strin imageUrl = String.format(
            Environment.getExternalStorageDirectory() + "/"
                    + getResources().getString(R.string.app_name)
                    + "/%s.png", "image(" + new_Date + ")");
    DealershipApplication.setVehicleImage(imageUrl);

    File photo = new File(imageUrl);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("exit", "false");
    intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    YourActivity.this.startActivityForResult(intent, REQUEST_CODE);

在manifest.xml中添加此权限。

    

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.FLASHLIGHT" />

<uses-feature
    android:name="android.hardware.camera.flash"
    android:required="false" />

<activity
    android:name="com.android.camera.Camera"
    android:clearTaskOnLaunch="true"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait"
    android:taskAffinity="android.task.camera"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

如果您有任何疑问,请与我们联系。

答案 1 :(得分:0)

在这种情况下,您必须调用Intent for Camera以及打开Gallery,之后您需要选择图像,然后您将在onActivityResult()中获得回调,在那里您将获得所选的图像路径,使用选择的图像路径,解码它,你会得到图像位图。

相关问题