###前提・実現したいこと
Android Studioを用いて、カメラで撮った写真の画像を表示させるアプリを作りたいと考えています。そのために、以下のサイトを参考に、プログラムを書きました。
http://tech.jsa.co.jp/how_to_get_storage_path/
しかし、サイトのように画像を読み込むことができなくて、困ってしまいました。どのような方法をとればうまく読み込むことができるようになるか、教えていただけたらと思います。お手数をお掛けしますが、よろしくお願いします。
###発生している問題・エラーメッセージ
まず、自分のAndroidスマートフォンの内部ストレージの「DCIM/Camera」のファイルの中に、アプリで表示させたいと考えている「DSC_0133.JPG」というカメラで撮った写真の画像を入れました。
その後、そのAndroidスマートフォンをUSBケーブルでパソコンを繋ぎ、アプリを実行させました。
ところが、「if(file.exists())」のところで画像を認識できていないらしく、else分の方に流れてしまうために画像を表示させることができないようなのです。
###該当のソースコード
「MainActivity」
package 個人情報が含まれているため省略
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView img = (ImageView)findViewById(R.id.img); String path = Environment.getExternalStorageDirectory().getPath(); System.out.println("パス="+ path); File dir = new File(path+"/DCIM/Camera/"); File file = new File(dir.getAbsolutePath()+"/DSC_0133.JPG"); if (file.exists()) { img.setImageBitmap(BitmapFactory.decodeFile(file.getPath())); }else{ System.out.println("失敗、ファイルのパス="+ file); } }
}
「activity_main.xml」
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=個人情報が含まれているため省略>
</RelativeLayout><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" />
###試したこと
画像の拡張子をJPGからjpgに変えるなどの方法を試してみたのですが、上手くいきませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
Androidバージョン=4.4.2
Android studio Version=2.2.1
回答1件
あなたの回答
tips
プレビュー