gpt4 book ai didi

java - 通过 Intent 拍摄照片

转载 作者:行者123 更新时间:2023-12-02 10:02:39 25 4
gpt4 key购买 nike

我正在尝试从 here 制作“拍照”教程遵循所有步骤后我仍然有错误。更确切地说,在我拍照后,它显示消息“相机已停止”。我添加了一个单击即可打开相机的按钮和一个用于显示照片的 imageView。我正在尝试在 Samsung Galaxy S7(Android 8.0.0,API 26)上部署该应用程序。

这是 setOnClickListener:

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById((R.id.button));
ImageView imageView = (ImageView)findViewById(R.id.imageView);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
button.setEnabled(false);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();

}
});
}

这是相机的方法:

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File imageView = null;
try {
imageView = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (imageView != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.camapp.fileprovider",
imageView);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}

文件路径:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_images"
path="Android/data/com.example.camapp/files/Pictures" />
</paths>

以及权限:

<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

提供商:

        <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.camapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>

我对 tutorial 中的 onActivityResult() createImageFile() 和 galleryAddPic() 有相同的方法.

这是堆栈跟踪:

java.lang.IllegalStateException: @NotNull method com/android/tools/idea/gradle/project/model/AndroidModuleModel.getRootDir must not return null
at com.android.tools.idea.gradle.project.model.AndroidModuleModel.$$$reportNull$$$0(AndroidModuleModel.java)
at com.android.tools.idea.gradle.project.model.AndroidModuleModel.getRootDir(AndroidModuleModel.java:571)
at com.android.tools.idea.gradle.project.sync.setup.module.android.ContentRootsModuleSetupStep.findContentEntries(ContentRootsModuleSetupStep.java:71)
at com.android.tools.idea.gradle.project.sync.setup.module.android.ContentRootsModuleSetupStep.doSetUpModule(ContentRootsModuleSetupStep.java:58)
at com.android.tools.idea.gradle.project.sync.setup.module.android.ContentRootsModuleSetupStep.doSetUpModule(ContentRootsModuleSetupStep.java:41)
at com.android.tools.idea.gradle.project.sync.setup.module.ModuleSetupStep.setUpModule(ModuleSetupStep.java:35)
at com.android.tools.idea.gradle.project.sync.setup.module.common.BaseSetup.setUpModule(BaseSetup.java:41)
at com.android.tools.idea.gradle.project.sync.ng.ModuleSetup.setupModuleModels(ModuleSetup.java:141)
at com.android.tools.idea.gradle.project.sync.ng.CachedProjectModelsSetup.setUpModules(CachedProjectModelsSetup.java:116)
at com.android.tools.idea.gradle.project.sync.ng.ProjectSetup$ProjectSetupImpl.setUpProject(ProjectSetup.java:82)
at com.android.tools.idea.gradle.project.sync.ng.SyncResultHandler.onSyncSkipped(SyncResultHandler.java:164)
at com.android.tools.idea.gradle.project.sync.ng.NewGradleSync.trySyncWithCachedGradleModels(NewGradleSync.java:219)
at com.android.tools.idea.gradle.project.sync.ng.NewGradleSync.sync(NewGradleSync.java:165)
at com.android.tools.idea.gradle.project.sync.ng.NewGradleSync.access$000(NewGradleSync.java:59)
at com.android.tools.idea.gradle.project.sync.ng.NewGradleSync$2.run(NewGradleSync.java:151)
at com.intellij.openapi.progress.impl.CoreProgressManager$TaskRunnable.run(CoreProgressManager.java:736)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:580)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:525)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:85)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
at com.intellij.openapi.progress.impl.CoreProgressManager$4.run(CoreProgressManager.java:395)
at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:314)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我想制作一个打开相机拍照的按钮,并将其保存在图库或其他指定目录中,因为我会对该照片进行一些图像处理。我尝试了 yt 上的几乎所有教程,我确实有一个很难找到这个问题的解决方案。我很感谢您提供的任何解决方案或建议。感谢您的宝贵时间!

最佳答案

如果您的应用需要相机点击图片,则应请求 CAMERA 权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">

<uses-permission android:name="android.permission.CAMERA"/>
<!-- other permissions go here -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application ...>
...
</application>

然后,您的应用应提示用户授予相机和存储权限

if (ContextCompat.checkSelfPermission(YourActivityName.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
YOUR_PERMISSIONS_REQUEST_CODE);

// YOUR_PERMISSIONS_REQUEST_CODE is an
// app-defined int constant. The callback method gets the
// result of the request.

} else {
// Permission has already been granted
}

同时在 list 文件中定义文件提供程序

 <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourPackageName.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_list"></meta-data>
</provider>

关于java - 通过 Intent 拍摄照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com