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

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

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

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

Android

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

Q&A

解決済

1回答

10334閲覧

androidアプリでカメラ、フォルダから画像を取得したい

n_kazuki

総合スコア25

Java

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

Android

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

0グッド

0クリップ

投稿2016/02/07 00:48

androidアプリで個人プロフィールの画像を設定するために、
カメラ及びフォルダから画像を取得し設定する処理を作成したいです。
参考サイトのコードをコピペして動作を確認しようとしましたが、
Permissionの問題で、動作しません。

エラー内容

java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=29585, uid=10129 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

manifestで下記のpermissionを追加しましたが、エラーは解消されませんでした。

xml

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

xml

1<intent-filter> 2 <action android:name="android.intent.action.VIEW"/> 3 <category android:name="android.intent.category.DEFAULT"/> 4 <data android:mimeType="image/png" /> 5</intent-filter>

同じ問題に出会った方がおりましたら、コメントしていただけると非常に助かります。

ソース

java

1import android.app.Activity; 2import android.content.ContentValues; 3import android.content.Intent; 4import android.media.MediaScannerConnection; 5import android.net.Uri; 6import android.os.Build; 7import android.os.Bundle; 8import android.provider.MediaStore; 9import android.view.View; 10import android.widget.Button; 11import android.widget.ImageView; 12 13 14public class MainActivity extends Activity { 15 16 private Uri m_uri; 17 private static final int REQUEST_CHOOSER = 1000; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 setViews(); 24 } 25 26 private void setViews() { 27 Button button = (Button) findViewById(R.id.button); 28 button.setOnClickListener(button_onClick); 29 } 30 31 private View.OnClickListener button_onClick = new View.OnClickListener() { 32 @Override 33 public void onClick(View v) { 34 showGallery(); 35 } 36 }; 37 38 private void showGallery() { 39 40 //カメラの起動Intentの用意 41 String photoName = System.currentTimeMillis() + ".jpg"; 42 ContentValues contentValues = new ContentValues(); 43 contentValues.put(MediaStore.Images.Media.TITLE, photoName); 44 contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 45 m_uri = getContentResolver() 46 .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); 47 48 Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 49 intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, m_uri); 50 51 // ギャラリー用のIntent作成 52 Intent intentGallery; 53 if (Build.VERSION.SDK_INT < 19) { 54 intentGallery = new Intent(Intent.ACTION_GET_CONTENT); 55 intentGallery.setType("image/*"); 56 } else { 57 intentGallery = new Intent(Intent.ACTION_OPEN_DOCUMENT); 58 intentGallery.addCategory(Intent.CATEGORY_OPENABLE); 59 intentGallery.setType("image/jpeg"); 60 } 61 Intent intent = Intent.createChooser(intentCamera, "画像の選択"); 62 intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{intentGallery}); 63 startActivityForResult(intent, REQUEST_CHOOSER); 64 } 65 66 @Override 67 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 68 super.onActivityResult(requestCode, resultCode, data); 69 70 if (requestCode == REQUEST_CHOOSER) { 71 72 if (resultCode != RESULT_OK) { 73 // キャンセル時 74 return; 75 } 76 77 Uri resultUri = (data != null ? data.getData() : m_uri); 78 79 if (resultUri == null) { 80 // 取得失敗 81 return; 82 } 83 84 // ギャラリーへスキャンを促す 85 MediaScannerConnection.scanFile( 86 this, 87 new String[]{resultUri.getPath()}, 88 new String[]{"image/jpeg"}, 89 null 90 ); 91 92 // 画像を設定 93 ImageView imageView = (ImageView) findViewById(R.id.image_view); 94 imageView.setImageURI(resultUri); 95 } 96 } 97}

xml

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:gravity="center" 7 tools:context=".MainActivity"> 8 9 <TextView android:text="Test Get Image from Gallery " 10 android:layout_margin="10dp" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" /> 13 <Button 14 android:id="@+id/button" 15 android:text="get image" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 19 <ImageView 20 android:id="@+id/image_view" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" /> 23 24</LinearLayout>

xml

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.hogehoge.myapplication" > 4 5 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:theme="@style/AppTheme" > 12 <activity 13 android:name=".MainActivity" 14 android:label="@string/app_name" > 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 <category android:name="android.intent.category.LAUNCHER" /> 18 </intent-filter> 19 <intent-filter> 20 <action android:name="android.intent.action.VIEW"/> 21 <category android:name="android.intent.category.DEFAULT"/> 22 <data android:mimeType="image/png" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27</manifest> 28

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

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

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

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

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

guest

回答1

0

ベストアンサー

設定アプリの自分のアプリを見てください。
外部ストレージの許可がオフになっている可能性があります。

投稿2016/02/07 01:05

yona

総合スコア18155

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

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

n_kazuki

2016/02/07 03:52

オフになっていました。 いつもコメントありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問