Androidアプリ開発でデータベースから画像を動的に表示する方法

このQ&Aのポイント
  • Androidアプリ開発でデータベースに保存された画像を動的に表示する方法を教えてください。
  • BitmapFactory.decodeResourceを使用して、res/drawableフォルダ内の画像を表示することはできますが、データベースに保存された画像を表示する方法が分かりません。
  • String型の変数に画像名を格納し、それを使ってBitmapFactory.decodeResourceを呼び出す方法を教えてください。
回答を見る
  • ベストアンサー

Androidアプリ開発に関する質問です。

Androidアプリ開発に関する質問です。 Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.xxx); という記述でres/drawable-hdpi/xxx.png の画像を持ってくることができると思うのですが、 データベースに保存したString型のxxxを使って動的に画像を表示する方法が分かりません。 データベースから画像名を持ってくる前に、String型の変数(仮にstrとする)に画像名を入れて、 Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.str); と記述してもstrを変数として扱ってくれません。 どなたかres/drawable/の画像を動的に持ってくる方法を教えて頂けないでしょうか。

  • Java
  • 回答数1
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
noname#177743
noname#177743
回答No.1

アプリ内のリソースとして用意されているイメージを利用するのであれば、例えばあらかじめ使用するイメージのリソース番号(R.drawable.xxxというやつ)を配列などにまとめておき、そこから必要に応じて取り出し処理すればいいと思います。 int[] res_data = {R.drawable.aaa, R.drawable.bbb……}; // リソース番号の配列を用意 int n = 取り出すデータ番号; Bitmap bmp = BitmapFactory.decodeResource(res_data[n]); あるいは、アプリ外にあるファイルを利用したいのであれば、decodeResourceを使わず、decodeFileを利用すればいいと思います。 Bitmap bmp = BitmapFactory.decodeFile(イメージファイルの絶対パス);

関連するQ&A

  • androidで読んだ画像サイズが小さくなります

    アンドロイドで画像を読み込んむと画像サイズが元のサイズより縮小されてしまいます。 画像の元の大きさで読み込むことはできないでしょうか? どなかたよろしければ、ご教授下さい。 256×256の画像を読み込み、以下のソースで確認してみました。 Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.画像の名前); Log.d("tag","w="+bmp1.getWidth()); Log.d("tag","h="+bmp1.bmp1.getHeight()); 結果 w=171 h=171 私は w=256 h=256 のまま読み込みして画面に貼り付けたいのですが。。。

  • 【Flash馬鹿のJava】連番画像ファイルの読込について

    【Flash馬鹿のJava】連番画像ファイルの読込について 対象バージョン:J2SE1.6 / Android1.6 はじめましてFlash馬鹿です。Java初心者です。 連番画像ファイルを一気に読み込みたいのですが、方法が分かりません。 Flashでいうところの for(i=1;i<100;i++){ this["loadmc" + i].loadMovie("image" + i + "png") } みたいなことがしたいです。 private Bitmap image1; private Bitmap image2; private Bitmap image3; image1 = BitmapFactory.decodeResource(r, R.drawable.image1); image2 = BitmapFactory.decodeResource(r, R.drawable.image2); image3 = BitmapFactory.decodeResource(r, R.drawable.image3); だといけますが private ArrayList<Bitmap> image; image.add(BitmapFactory.decodeResource(r, R.drawable.image1)); image.add(BitmapFactory.decodeResource(r, R.drawable.image2)); image.add(BitmapFactory.decodeResource(r, R.drawable.image3)); にするとAndroid1.6のシミュレータが落ちます。 また、連番の画像リソースを指定する方法("image" + i + "png")も分かりませんので 教えていただけると幸いです。 よろしくお願いします。

    • ベストアンサー
    • Java
  • java-Eclipseエラー質問

    Eclips SDKを使用して、Androidのアプリの開発練習をしています。 エラーが出てしまい解決方法を数日間検索しているのですが、 見当たらず、わからないので、質問させていただきます。 バージョンは Eclipse SDK Version: 4.2.2 Build id: M20130204-1200 を使用しています。 エラーは m_Number[0] = BitmapFactory.decodeResource(r, R.drawable.num1); m_Number[1] = BitmapFactory.decodeResource(r, R.drawable.num2); m_Number[2] = BitmapFactory.decodeResource(r, R.drawable.num3); m_Number[3] = BitmapFactory.decodeResource(r, R.drawable.num4);   ・・・・・ の位置で、 メソッド decodeResource(Resources, Object) は型 String で未定義です。 と表示されます。 ソースは以下のように作っています。 package com.example.puzzle15; import android.os.Bundle; import android.app.Activity; import android.view.Menu; //public class MainActivity extends Activity { // @Override // protected void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // } // @Override // public boolean onCreateOptionsMenu(Menu menu) { // // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); // return true; // } //} //class : PuzzleView //Abstract : パズルを表示する public class PuzzleView extends SurfaceView implements SurfaceHolder.Callback{ private static final String BitmapFactory = null; private Bitmap m_Number[] = new Bitmap[15]; private SurfaceHolder m_holder; private final int m_PosX[] = { 0, 80, 160, 240 }; private final int m_PosY[] = { 0, 80, 160, 240 }; private final int m_IdxX[] = {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3}; private final int m_IdxY[] = {0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3}; // コンストラクタ // サーフェースホルダーとイメージの準備 public PuzzleView(Context context) { super(); Resources r = getResources(); // 画像リソースの取得 m_Number[0] = BitmapFactory.decodeResource(r, R.drawable.num1); m_Number[1] = BitmapFactory.decodeResource(r, R.drawable.num2); m_Number[2] = BitmapFactory.decodeResource(r, R.drawable.num3); m_Number[3] = BitmapFactory.decodeResource(r, R.drawable.num4); m_Number[4] = BitmapFactory.decodeResource(r, R.drawable.num5); m_Number[5] = BitmapFactory.decodeResource(r, R.drawable.num6); m_Number[6] = BitmapFactory.decodeResource(r, R.drawable.num7); m_Number[7] = BitmapFactory.decodeResource(r, R.drawable.num8); m_Number[8] = BitmapFactory.decodeResource(r, R.drawable.num9); m_Number[9] = BitmapFactory.decodeResource(r, R.drawable.num10); m_Number[10] = BitmapFactory.decodeResource(r, R.drawable.num11); m_Number[11] = BitmapFactory.decodeResource(r, R.drawable.num12); m_Number[12] = BitmapFactory.decodeResource(r, R.drawable.num13); m_Number[13] = BitmapFactory.decodeResource(r, R.drawable.num14); m_Number[14] = BitmapFactory.decodeResource(r, R.drawable.num15); // サーフェースホルダーの生成 m_holder = getHolder(); m_holder.addCallback(this);// サーフェース生成時と変更時、終了時に呼び出してもらう } private SurfaceHolder getHolder() { // TODO Auto-generated method stub return null; } private Resources getResources() { // TODO Auto-generated method stub return null; } //SurfaceCreated //サーフェース生成時処理 public void surfaceCreated(SurfaceHolder holder){ } //SurfaceChanged //サーフェース変更時処理 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { int m_XOrg; int m_YOrg; boolean m_bLand; if( height > width ){ m_XOrg = (width - 320) / 2; m_YOrg = 32; m_bLand = false; } else{ m_XOrg = 32; m_YOrg = (height - 320 ) / 2; m_bLand = true; } m_holder = holder; Thread m_Thread = new Thread((Runnable) this); m_Thread.start(); updateView(); Canvas canvas = holder.lockCanvas(); canvas.drawColor(Color.BLACK); for( int i = 0 ; i < 15 ; i++ ){ canvas.drawBitmap(m_Number[i], m_PosX[m_IdxX[i]], m_PosY[m_IdxY[i]], null); } holder.unlockCanvasAndPost(canvas); } private void updateView() { // TODO Auto-generated method stub } // SurfaceDestroyed // サーフェース終了時処理 public void surfaceDestroyed(SurfaceHolder holder){ } } よろしくお願いします。

    • ベストアンサー
    • Java
  • androidアプリのlistviewについて質問

    現在Listviewにてテキストと画像を表示させています。 各行をクリックした時に、それぞれ別のActivityに移行させたいのですが、 初心者の為、悪戦苦闘しています。 わかる方がいましたら、アドバイスをお願い致します。 現在のソースは以下のとおりで、各行をタッチしても何も起こらない状況です。 public class CustomListViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap image; image = BitmapFactory.decodeResource(getResources(), R.drawable.tb); Bitmap image2; image2 = BitmapFactory.decodeResource(getResources(), R.drawable.tb2); Bitmap image3; image3 = BitmapFactory.decodeResource(getResources(), R.drawable.tb3); List<CustomData> objects = new ArrayList<CustomData>(); CustomData item1 = new CustomData(); item1.setImagaData(image); item1.setTextData("1行目のテキスト"); CustomData item2 = new CustomData(); item2.setImagaData(image2); item2.setTextData("2行目のテキスト"); CustomData item3 = new CustomData(); item3.setImagaData(image3); item3.setTextData("3行目のテキスト"); objects.add(item1); objects.add(item2); objects.add(item3); CustomAdapter customAdapater = new CustomAdapter(this, 0, objects); ListView listView = (ListView)findViewById(R.id.list); listView.setAdapter(customAdapater); } protected void onListItemClick(ListView l, View v, int position, long id) { switch (position) { case 0: startActivity(new Intent(this, ItemDetailActivity.class)); break; case 1: startActivity(new Intent(this, ItemDetailActivity.class)); break; case 2: startActivity(new Intent(this, ItemDetailActivity.class)); break; } } }

  • クリックして画像を変え、一定時間経過後もとに戻す

    androidアプリを作成している初心者です。 画像をクリックすると画像が変わるようになるようにしたいと思ってます。 画像を変えるところまではできたのですが、 一定時間経過後にもとに戻すにはどうしたらいいかがわかりません。 以下、ソースコード。 public class MainActivity extends Activity { ImageView iv; Bitmap bmp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setGravity(Gravity.CENTER); setContentView(ll); //画像表示 iv = new ImageView(this); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ikari); iv.setImageBitmap(bmp); ll.addView(iv); //画像をクリックした時に関数を呼び出す iv.setOnClickListener(new img_ac()); } //画像を変える class img_ac implements OnClickListener { public void onClick(View v) { ImageView tmp = (ImageView) v; tmp.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); } } } 一定時間経過後に元の画像に戻すには、どのようにしたら良いのでしょうか。

    • ベストアンサー
    • Java
  • Android用ゲームを作っています。

    現在Androidゲームを作っているのですが、現在時間を取得し、時刻を表示したり、背景画像などを変えたりしたいのですがどのように取得し、表示させればいいのでしょうか?ソースコードは ・MainActivity.java package andorid.eample.game2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.os.Bundle; import android.app.Activity; import android.widget.LinearLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout l = new LinearLayout(this); setContentView(l); l.addView(new PView(this, null, 0)); } } ・PView.java package andorid.eample.game2; import android.annotation.SuppressLint;4 import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import android.content.res.Resources; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.text.format.Time; import android.media.MediaPlayer; public class PView extends View{ Paint paint = new Paint(); Date date = new Date(); int flg = 0; int charaX =250; int charaY = 250; Resources res = this.getContext().getResources(); Bitmap bg = BitmapFactory.decodeResource(res, R.drawable.bg1); Bitmap chara = BitmapFactory.decodeResource(res, R.drawable.charan); Bitmap charaT = BitmapFactory.decodeResource(res, R.drawable.charat); Bitmap box= BitmapFactory.decodeResource(res, R.drawable.boxt); //音楽読み込み MediaPlayer se = MediaPlayer.create(getContext(),R.raw.ehehe); public PView(Context context,AttributeSet attrs,int defStyle) { super(context,attrs); // TODO 自動生成されたコンストラクター・スタブ } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas c){ c.drawBitmap(bg,0,100,paint); if(flg == 0) { c.drawBitmap(chara,charaX,charaY,paint); } if(flg == 1) { c.drawBitmap(charaT,charaX,charaY,paint); c.drawBitmap(box,50,200,paint); } invalidate(); } public boolean onTouchEvent(MotionEvent me) { //タッチされた時 if(me.getAction() == MotionEvent.ACTION_DOWN) { if(me.getAction() == MotionEvent.ACTION_DOWN) { if(flg == 0) { flg = 1; se.start(); } else{ flg =0; } } } return false; } } このような感じになっています どうかアドバイスをお願いします。

    • ベストアンサー
    • Java
  • Androidプログラムについて

    Androidのプログラムについてです。 パラパラ漫画のように、多数の画像を読み込んで表示させるプログラムを教えて下さい。 サンプルを見つけましたが、画像数が極端に多い場合、for文などで出来るのでしょうか。 サンプルは下記で見つけました。 http://monoist.atmarkit.co.jp/mn/articles/1205/21/news003.html 画像の読み込みや、 画像をアニメーションのコマとして追加していく方法が記載されていますが、 画像が多い場合、for文などに変更できるのでしょうか。 (プログラム全く初心者です。) <参考> 上記URLには以下サンプルプログラムが記載されています。 // 画像の読み込み // Drawable frame1 = con.getResources().getDrawable( R.drawable.frame1 ); Drawable frame2 = con.getResources().getDrawable( R.drawable.frame2 ); Drawable frame3 = con.getResources().getDrawable( R.drawable.frame3 ); Drawable frame4 = con.getResources().getDrawable( R.drawable.frame4 ); Drawable frame5 = con.getResources().getDrawable( R.drawable.frame5 ); // 画像をアニメーションのコマとして追加していく anim.addFrame( frame1, 60 ); anim.addFrame( frame2, 60 ); anim.addFrame( frame3, 70 ); anim.addFrame( frame4, 80 ); anim.addFrame( frame5, 120 ); anim.addFrame( frame4, 80 ); anim.addFrame( frame3, 70 ); anim.addFrame( frame2, 60 ); anim.addFrame( frame1, 60 ); <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/frame1" android:duration="60" /> <item android:drawable="@drawable/frame2" android:duration="60" /> <item android:drawable="@drawable/frame3" android:duration="70" /> <item android:drawable="@drawable/frame4" android:duration="80" /> <item android:drawable="@drawable/frame5" android:duration="120" /> <item android:drawable="@drawable/frame4" android:duration="80" /> <item android:drawable="@drawable/frame3" android:duration="70" /> <item android:drawable="@drawable/frame2" android:duration="60" /> <item android:drawable="@drawable/frame1" android:duration="60" /> </animation-list> .

    • ベストアンサー
    • Java
  • listviewの画面遷移について

    初心者です。 現在カスタムlistviewにて各行に画像とテキストを表示させており、各行をクリックした時に別のActivityに移行させたいのですが、下記ソースにてクリックしても何も動作がしない状態です。 どうかご指南をお願い致します。 public class CustomListViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap image; image = BitmapFactory.decodeResource(getResources(), R.drawable.tb); Bitmap image2; image2 = BitmapFactory.decodeResource(getResources(), R.drawable.tb2); Bitmap image3; image3 = BitmapFactory.decodeResource(getResources(), R.drawable.tb3); List<CustomData> objects = new ArrayList<CustomData>(); CustomData item1 = new CustomData(); item1.setImagaData(image); item1.setTextData("1行目のテキスト"); CustomData item2 = new CustomData(); item2.setImagaData(image2); item2.setTextData("2行目のテキスト"); CustomData item3 = new CustomData(); item3.setImagaData(image3); item3.setTextData("3行目のテキスト"); objects.add(item1); objects.add(item2); objects.add(item3); CustomAdapter customAdapater = new CustomAdapter(this, 0, objects); ListView listView = (ListView)findViewById(R.id.list); listView.setAdapter(customAdapater); } protected void onListItemClick(ListView l, View v, int position, long id) { switch (position) { case 0: startActivity(new Intent(this, Item1Activity.class)); break; case 1: startActivity(new Intent(this, Item2Activity.class)); break; case 2: startActivity(new Intent(this, Item3Activity.class)); break; } } }

  • 現在AndroidSDKにてゲームを製作して

    いるのですが、ゲーム画面は出来ているのですが、ゲームスタート画面、ゲームオーバー画面を作りたいのですがどうすればよろしいのでしょうか、スタート画面はタッチされたらゲーム画面に切り替わるようにしたいです。 ソースは メインアクティビティーは package android.game; import android.os.Bundle; import android.app.Activity; import android.widget.LinearLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout l = new LinearLayout(this); setContentView(l); l.addView(new textview(this, null, 0)); l.addView(new PView(this, null, 0)); } } で、ゲーム画面のクラスは package android.game; import android.annotation.SuppressLint; import android.content.Context; import android.game.R.id; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.Random; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Paint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import java.util.Random; public class PView extends View{ Paint paint = new Paint(); int player = 0; int playerV = 20; int tamaV = 20; int x = 100; int y = 50; int tamaY = player; int tamaX = player; int line = 550; int tama2X = 0; int tama2Y = 0; int linex = 400; int cnt = 0; int score = 0; int timerT = 30; int flg = 0; int stx = 200; int sty = 200; Random rand = new Random(); Resources res = this.getContext().getResources(); Bitmap bg = BitmapFactory.decodeResource(res, R.drawable.haikei); Bitmap pl = BitmapFactory.decodeResource(res, R.drawable.player); Bitmap tama = BitmapFactory.decodeResource(res, R.drawable.tama); Bitmap tama2 = BitmapFactory.decodeResource(res, R.drawable.tama2); Bitmap lineview = BitmapFactory.decodeResource(res, R.drawable.line); Bitmap lineview2 = BitmapFactory.decodeResource(res, R.drawable.line2); public PView(Context context,AttributeSet attrs,int defStyle) { super(context,attrs); // TODO 自動生成されたコンストラクター・スタブ } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas c){ timerT--; if(timerT < 0) { timerT = 0; } player += playerV; if(player<0 || 400<player) playerV *= -1; //弾の処理 tamaY+= 0; tama2Y += 20; if(tamaY > getHeight()) { tamaY = rand.nextInt(500)+1; } tamaX = rand.nextInt(30); tama2Y += 20; if(tama2Y > getHeight()) { tama2Y = rand.nextInt(500)+1; } tama2X = rand.nextInt(100); c.drawBitmap(bg,0,0,paint); c.drawBitmap(pl,player,y, paint); c.drawBitmap(tama,player,tamaY,paint); c.drawBitmap(tama2,tama2X,tama2Y, paint); c.drawBitmap(lineview,0,line,paint); c.drawBitmap(lineview2,0,linex, paint); invalidate(); } public boolean onTouchEvent(MotionEvent me) { //タッチされた時 if(me.getAction() == MotionEvent.ACTION_DOWN) stx = 0; { if(me.getAction() == MotionEvent.ACTION_DOWN) { if(tamaY >= line && tamaY < line + 40) { tamaY = 0; score+= 10; } } if(me.getAction() == MotionEvent.ACTION_DOWN){ if(tama2Y >= linex && tama2Y < linex + 50) { tama2Y = 0; score+= 20; } } return true; } } } このようになっています。 よろしくお願いします

  • Androidのbitmap表示について

    Androidのbitmap表示について res フォルダ内に drawable というフォルダをつくり、そこに 100pixel (チェック) 100pixel の画像を入れ込んでいます。 そして、bitmapを作成して c.drawBitmap( bitmap0, 0 , 0 , null ); のような感じで表示しているのですが、サイズが100(チェック)100になっていないようなのです。 (勝手に大きくなってる?) なぜでしょうか?

    • ベストアンサー
    • Java

専門家に質問してみよう