So currently i have a list that shows images and text beside it but having a problem showing pictures. I thought that the best approach is to parse using JSOUP then download image and decode it using bitmapsFactory but failed because it takes too much memory still displaying the pictures. Is there a way to display the image online so it does not take space? i thought about using Webview and then insert the link and display the picture but would that work? I included my MainActivity, ListClass, ImageHandler and XML for the list.
Main Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView list;
imageHandler imagess = new imageHandler();
String[] web = {"Egypt "};
Drawable[] imageId = { imagess.LoadImageFromWebOperations("http://egyptianstreets.com/wp-content/uploads/2016/08/satelliteegypt-400x240.jpg")};
Handler handler;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
listClass adapter = new
listClass(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
ListClass;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class listClass extends ArrayAdapter<String>{
//Next three lines introduces Variables to use in the class
private final Activity context;
private final String[] web;
private final Drawable[] imageId;
//Constructor to get Web, Image and context to set adapter
public listClass(Activity context, String[] web, Drawable[] imageId) {
super(context, R.layout.list, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
//Public method to ...
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageDrawable(imageId[position]);
return rowView;
}
}
ImageHandler using InputStream but does not work;
import android.media.Image;
import java.net.URL;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import android.graphics.drawable.Drawable;
public class imageHandler {
Image picture;
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "earthdownload");
return d;
} catch (Exception e) {
return null;
}
}
}
XML for list;;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img"
android:layout_width="150dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/txt"
android:paddingRight="16dp"
android:textSize="15dp"
android:layout_alignLeft="@+id/img"
android:layout_marginLeft="150dp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"/>
</RelativeLayout>