[Android] List image view - How should I load the image file?
- List example site: https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/ListView.txt
(Thanks to Dear noel, EmmanuelPil.)
import android.app.Activity;
import android.widget.FrameLayout;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnClickListener;
import android.R;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.content.Context;
import android.view.LayoutInflater;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.widget.TextView;
import android.view.ViewGroup;
import android.graphics.BitmapFactory;
import android.os.Environment;
// import android.widget.RelativeLayout.LayoutParams;
//import android.app.Activity;
//import android.graphics.Color;
//import android.os.Bundle;
//import android.view.View;
//
//
String[] myStringArray = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh",
"Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth"};
Activity act;
FrameLayout fl;
ListView listView;
int padding_left;
int padding_right;
int padding_bottom;
int padding_top;
PImage img;
String image_folder = "list_image_view";
String url = ""; // new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
void findImage(String sf, String tf) {
try {
String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(absolute_path+"/"+sf+"/"+tf);
println("file : " + file);
if (!file.exists()) {
// boolean success = true;
// success = file.mkdirs();
println("File not find.");
}else{
println("File find.");
}
}
catch (Exception e) {
println("Error : " + e);
}
}
void setup() {
println("step-0-1");
// url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/image.png";
act = this.getActivity();
println("step-0-2");
// img = loadImage(url);
url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
println(" url : " + url);
findImage(image_folder,"image.png");
fullScreen();
background(255, 190, 10);
fill(0);
textAlign(CENTER, CENTER);
String str = "Click on screen to show listView.";
textSize(12);
float twf = width/textWidth(str);
textSize(9*twf);
text(str, width/2, height/2);
println("step-0-3");
}
void draw() {
}
void mousePressed() {
act.runOnUiThread(
new Runnable() {
public void run() {
background(255, 190, 10);
pushStyle();
stroke(160, 60, 0);
strokeWeight(20);
noFill();
rectMode(CENTER);
rect(width/2, height/2, width-2*padding_left, height-2*padding_top);
popStyle();
listView.setVisibility(View.VISIBLE);
}
}
);
}
//@ Override
public void onStart() {
super.onStart();
act = this.getActivity();
padding_left = width/6;
padding_right = width/6;
padding_bottom = height/6;
padding_top = height/6;
ArrayAdapter adapter = new ArrayAdapter<String>(act, android.R.layout.simple_list_item_1, myStringArray);
listView = new ListView(act);
listView.setLayoutParams(new RelativeLayout.LayoutParams(width-padding_left-padding_right, height-padding_top-padding_bottom));
listView.setAdapter(adapter);
listView.setBackgroundColor(Color.rgb(255, 230, 130));
listView.setX(padding_left);
listView.setY(padding_top);
//listView.setPadding(padding_left, padding_top, padding_right, padding_bottom);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object list = listView.getItemAtPosition(position);
background(255, 190, 10);
text("You have chosen item: "+list, width/2, height/2);
text("Click to call list again.", width/2, height/2+100);
listView.setVisibility(View.GONE);
}
}
);
listView.setVisibility(View.GONE);
fl = (FrameLayout)act.findViewById(R.id.content);
fl.addView(listView);
MyAdapter adapter2 = new MyAdapter(act, myStringArray);
listView.setAdapter(adapter2);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] values) {
super(context, android.R.layout.simple_list_item_1, values);
}
// Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("step-1");
//try {
// url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
// System.out.println("step-2 "+url);
//}catch (Exception e) { }
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
System.out.println("step-3");
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(getItem(position));
ImageView imageView = new ImageView(getContext());
AssetManager am = getResources().getAssets() ;
InputStream is = null ;
System.out.println("step-4");
try {
// 애셋 폴더에 저장된 field.png 열기.
System.out.println("step-5");
is = am.open(url) ;
Bitmap bm = BitmapFactory.decodeStream(is);
System.out.println("step-6");
// 만들어진 Bitmap 객체를 이미지뷰에 표시.
imageView.setImageBitmap(bm) ;
is.close() ;
} catch (Exception e) {
e.printStackTrace();
}
if (is != null) {
try {
is.close() ;
} catch (Exception e) {
e.printStackTrace() ;
}
}
// imageView = (ImageView) findViewById(R.id.image1) ;
// imageView.setImageResource(bm);
// imageView.setImageResource(img);
imageView.setLayoutParams(new LinearLayout.LayoutParams(100, 100)); // set the size of the image here
LinearLayout layout = (LinearLayout) view;
layout.addView(imageView);
return view;
}
}
url : /storage/emulated/0/list_image_view/image.png
file : /storage/emulated/0/list_image_view/image.png
File not find.
there is a problem.
- processing(android) folder / data / image_sample.png
- I need to read ‘image_sample.png’, but I don’t know where the file is located.
Is there any way to find it?