質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

0回答

536閲覧

ボタンを押して、Intentから既存のカメラを呼び出して保存したい。

RON__

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2022/08/18 08:25

現在、ボタンを押してIntentで既存のカメラアプリを呼び出し、そこで撮った画像を保存するアプリ(?)を作っています。
ImageView,button.setOnclickListenerの部分を省略すれば、エラーは起こりませんがデバックできませんでした。
インテント汚くて申し訳ありませんが、よろしくお願いします。

実現したいこと

ボタンを押してIntentで既存のカメラアプリを呼び出し、そこで撮った画像を保存すること。

発生している問題・エラーメッセージ

Cannot resolve method 'createImageFile' in 'MainActivity ';' expected' Expression expected シンボル 'Expression' を解決できません Unhandled exception: java.io.IOException Cannot return a value from a method with void result type Annotations are not allowed here Identifier or type expected シンボル 'data' を解決できません Unexpected token '}' expected

MainActivity

java

1package com.example.minmo6; 2 3import androidx.appcompat.app.AppCompatActivity; 4import androidx.core.content.FileProvider; 5 6import android.content.Intent; 7import android.net.Uri; 8import android.os.Bundle; 9import android.os.Environment; 10import android.provider.MediaStore; 11import android.widget.Button; 12import android.widget.ImageView; 13 14import java.io.File; 15import java.io.IOException; 16import java.text.SimpleDateFormat; 17import java.util.Date; 18 19public class MainActivity extends AppCompatActivity { 20 21 private final static int REQUEST_CAMERA = 714; 22 private ImageView imageView; 23 24 public String currentPhotoPath; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 imageView = findViewById(R.id.image_view); 32 33 Button button1 = findViewById(R.id.button1); 34 35 button1.setOnClickListener(v -> { 36 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 37 if (intent.resolveActivity(getPackageManager()) != null) { 38 File PhotoFile = null; 39 try { 40 PhotoFile = createImageFile(); 41 } catch (IOException ex) { 42 } 43 44 if (PhotoFile != null) { 45 Uri photoURI = FileProvider.getUriForFile(MainActivity.this, 46 getApplicationContext().getPackageName() + ".fileprovider", 47 PhotoFile); 48 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 49 startActivityForResult(intent, REQUEST_CAMERA); 50 } 51// startActivityForResult(intent, REQUEST_CAMERA); 52 } 53 }); 54 55 private File createImageFile() Expression expectedthrows IOException { 56 // Create an image file name 57 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 58 String imageFileName = "JPEG_" + timeStamp + "_"; 59 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 60 File image = File.createTempFile( 61 imageFileName, // prefix 62 ".jpg", // suffix 63 storageDir // directory 64 ); 65 66 currentPhotoPath = image.getAbsolutePath(); 67 return image; 68 } 69 70 @Override 71 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 72 super.onActivityResult(requestCode, resultCode, data); 73 // リクエストコードがREQUEST_CAMERAであるか確認 74 if (requestCode == REQUEST_CAMERA) { 75 if( data.getExtras() != null){ 76 // dataから画像を取り出す 77 } 78 } 79 } 80}

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/Title" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:rotation="0" 14 android:text="App" 15 android:textSize="24pt" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:layout_constraintLeft_toLeftOf="parent" 18 app:layout_constraintRight_toRightOf="parent" 19 app:layout_constraintTop_toTopOf="parent" 20 app:layout_constraintVertical_bias="0.4" /> 21 22 <Button 23 android:id="@+id/button1" 24 style="@style/Widget.AppCompat.Button" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:backgroundTint="#000000" 28 android:text="写真を撮影" 29 android:textSize="34sp" 30 app:layout_constraintBottom_toBottomOf="parent" 31 app:layout_constraintHorizontal_bias="0.157" 32 app:layout_constraintLeft_toLeftOf="parent" 33 app:layout_constraintRight_toRightOf="parent" 34 app:layout_constraintTop_toTopOf="parent" 35 app:layout_constraintVertical_bias="0.866" 36 app:strokeColor="#000000" /> 37 38 <ImageView 39 android:id="@+id/image_view" 40 android:layout_width="300dp" 41 android:layout_height="534dp" 42 android:contentDescription="@string/description" 43 android:scaleType="fitCenter" 44 app:layout_constraintBottom_toBottomOf="parent" 45 app:layout_constraintHorizontal_bias="0.495" 46 app:layout_constraintLeft_toLeftOf="parent" 47 app:layout_constraintRight_toRightOf="parent" 48 app:layout_constraintTop_toTopOf="parent" 49 app:layout_constraintVertical_bias="0.152" /> 50 51</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.minmo6"> 4 5 <uses-permission android:name="android.permission.CAMER"/> 6 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 7 8 <application 9 android:allowBackup="true" 10 android:icon="@mipmap/ic_launcher" 11 android:label="@string/app_name" 12 android:roundIcon="@mipmap/ic_launcher_round" 13 android:supportsRtl="true" 14 android:theme="@style/Theme.Minmo6"> 15 16 <provider 17 android:name="androidx.core.content.FileProvider" 18 android:authorities="${applicationId}.fileprovider" 19 android:exported="false" 20 android:grantUriPermissions="true"> 21 <meta-data 22 android:name="android.support.FILE_PROVIDER_PATHS" 23 android:resource="@xml/provider_path"/> 24 </provider> 25 26 <activity 27 android:name=".MainActivity" 28 android:exported="true"> 29 <intent-filter> 30 <action android:name="android.intent.action.MAIN" /> 31 32 <category android:name="android.intent.category.LAUNCHER" /> 33 </intent-filter> 34 </activity> 35 </application> 36</manifest>

試したこと

MainActivityの

java

1 imageView = findViewById(R.id.image_view); 2 3 Button button1 = findViewById(R.id.button1); 4 5 button1.setOnClickListener(v -> {

の部分を消すと、前述したエラーたちは消えましたが、Android Studio上ではデバックできませんでした。

---その時のエラーコード

One or more issues found when checking AAR metadata values: Dependency 'androidx.appcompat:appcompat-resources:1.5.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.appcompat:appcompat:1.5.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.emoji2:emoji2-views-helper:1.2.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.emoji2:emoji2:1.2.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31'

補足情報(FW/ツールのバージョンなど)

実行環境
Android Studio Bumblebee | 2021.1.1 Patch 2
Build #AI-211.7628.21.2111.8193401, built on February 17, 2022
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 12
Registry: external.system.auto.import.disabled=true

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2022/08/19 12:07

エラーメッセージを正しく解釈してコードを修正してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問