[Android] ListView imageview - How should I load the image file?

[Android] List image view - How should I load the image file?

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.

  1. processing(android) folder / data / image_sample.png
  2. I need to read ‘image_sample.png’, but I don’t know where the file is located.

Is there any way to find it?

I was unable to run the posted code. It’s pretty simple if all you want is an image; just place the image in a ‘data’ folder inside of your sketch folder.

PImage img;

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(209);
  // Needs to be inside 'data' folder
  img = loadImage("myImage.jpg");
}

void draw() {
  image(img, 480, 480, 300, 300);
}
1 Like

@svan

Dear svan

Nice to meet you. thank you

‘Bitmap bm’ An image must be assigned to the corresponding variable.
How can I put a picture file in ‘Bitmap’ in ‘Processing’?

The answer is here.

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";

String cheminComplet="";
String imageName="";




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");
  
  
   
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
  // println(" url : " + url);
  // findImage(image_folder,"image.png");
  
  img = loadImage("image.png");
  File externalDir = Environment.getExternalStorageDirectory();
  String path = externalDir.getAbsolutePath();
  cheminComplet = path+File.separator;
  imageName = "image_sample.png";
  img.save(path+File.separator+imageName);
  
  
  
  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 Bitmap createBitmap(String in){////could be better to create an handler for that
 
  Bitmap b = null;
  b = BitmapFactory.decodeFile(cheminComplet+in); 
return b;
}



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(0); // 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) ; 
            // is = am.open("image.png") ;             
            // Bitmap bm = BitmapFactory.decodeStream(is);
            
            Bitmap bm = createBitmap(imageName) ;
            if( bm == null ) { println(" not image!"); } 
            
    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() ;
            }
        }
    System.out.println("step-7");
    // 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
    System.out.println("step-8");
    LinearLayout layout = (LinearLayout) view;
    System.out.println("step-9");
    layout.addView(imageView);
    System.out.println("step-10");
    
    
    return view;
  }
}

An execution error pops up.

FATAL EXCEPTION: main
Process: processing.test.list_image_view, PID: 19185
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
at processing.test.list_image_view.list_image_view$MyAdapter.getView(list_image_view.java:308)
at android.widget.AbsListView.obtainView(AbsListView.java:3219)
at android.widget.ListView.makeAndAddView(ListView.java:2219)

link: android - how to create listview ui like this - Stack Overflow

I want to implement ‘image listview’ through ‘processing android’. Does anyone know?

Have you seen this demo: https://github.com/vsquared/AndroidWidgets_Processing4 . There is a listView demonstrated here and should be customizable.

It’s also possible to create xml code with JavaFX; I have no experience (yet). SceneBuilder (free) by Gluon will also create xml code for mobile devices (drag and drop).

1 Like

@svan

Thank you for the good content (source code).