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

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

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

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

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

886閲覧

drawableではなくassetsで画像表示させたい

JavaBi

総合スコア0

Java

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

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/01/21 03:30

drawableから画像を取り出してgridview表示するプログラムを作成したのですが、assetsフォルダーの下にあるphotosから画像を取り出して表示するプログラムに書き換えたいと思っています。
そこで次のプログラムをどのように書き換えればいいのかを教えていただきたいです。

MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener {

private AssetManager assetManager; private final static String TAG = "MainActivity"; ArrayList<String> listPath; ArrayList<Bitmap> bitmapList; // 表示する画像の名前(拡張子無し) private String[] members = { "yuuki", "kana", "minami" }; // Resource IDを格納するarray private List<Integer> imgList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // for-each member名をR.drawable.名前としてintに変換してarrayに登録 for (String member: members){ int imageId = getResources().getIdentifier( member,"photos", getPackageName()); imgList.add(imageId); } // GridViewのインスタンスを生成 GridView gridview = findViewById(R.id.gridview); // BaseAdapter を継承したGridAdapterのインスタンスを生成 // 子要素のレイアウトファイル grid_items.xml を // activity_main.xml に inflate するためにGridAdapterに引数として渡す GridAdapter adapter = new GridAdapter(this.getApplicationContext(), R.layout.grid_items, imgList, members ); // gridViewにadapterをセット gridview.setAdapter(adapter); // item clickのListnerをセット gridview.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(getApplication(), SubActivity.class); intent.putExtra("IMAGEID", imgList.get(position)); startActivity( intent ); }

}

GridAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class GridAdapter extends BaseAdapter {

class ViewHolder { ImageView imageView; TextView textView; } private List<Integer> imageList = new ArrayList<>(); private String[] names; private LayoutInflater inflater; private int layoutId;

SubActivity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class SubActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); Intent intent = getIntent(); int imageId = intent.getIntExtra("IMAGEID", 0); ImageView imageView = findViewById(R.id.image_view); imageView.setImageResource(imageId); }

}

activity_main

<?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:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp" android:background="#aaa" tools:context=".MainActivity">

<GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#888" android:gravity="center" android:horizontalSpacing="1dp" android:verticalSpacing="1dp" android:numColumns="5" android:stretchMode="columnWidth" />
</RelativeLayout>

activity_sub

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:contentDescription="@string/description" />
</LinearLayout>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問