Load Webp From Cyberspace Together With Display Inward Listview

Posted on

WebP is a modern picture format that provides superior lossless in addition to lossy compression for images on the web. Details advert Google Developers – WebP.

WebP is supported starting from Android 4.0+ (reference: Async charge picture from network to ListView” to charge WebP from network in addition to display inward ListView. Once exceptional clicked, role “Simplest agency to opened upwards browser using CustomTabsIntent.Builder“.


To use CustomTabsIntent.Builder inward our app, To role CustomTabsIntent.Builder, edit app/build.gradle to add together dependencies of compile ‘com.android.support:customtabs:23.0.0’.

The WebP images charge from the page Google Developers – WebP Image Galleries.

MainActivity.java

package com.blogspot.android_er.androidimage;  import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.customtabs.CustomTabsIntent; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;  import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays;  world shape MainActivity extends AppCompatActivity {      concluding static String src[] = {             "https://www.gstatic.com/webp/gallery3/1_webp_ll.webp",             "https://www.gstatic.com/webp/gallery3/1_webp_a.webp",             "https://www.gstatic.com/webp/gallery3/2_webp_ll.webp",             "https://www.gstatic.com/webp/gallery3/2_webp_a.webp",             "https://www.gstatic.com/webp/gallery3/3_webp_ll.webp",             "https://www.gstatic.com/webp/gallery3/3_webp_a.webp",             "https://www.gstatic.com/webp/gallery3/4_webp_ll.webp",             "https://www.gstatic.com/webp/gallery3/4_webp_a.webp",             "https://www.gstatic.com/webp/gallery3/5_webp_ll.webp",             "https://www.gstatic.com/webp/gallery3/5_webp_a.webp" };      ListView imageList;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          imageList = (ListView) findViewById(R.id.imagelist);         ArrayList<String> srcList = novel ArrayList<String>(Arrays.asList(src));         imageList.setAdapter(new CustomListAdapter(this, srcList));          imageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {             @Override             world void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 String imageSrc = src[position];                 Toast.makeText(MainActivity.this,                         imageSrc,                         Toast.LENGTH_LONG).show();                  Uri imageUri = Uri.parse(imageSrc);                 novel CustomTabsIntent.Builder()                         .build()                         .launchUrl(MainActivity.this, imageUri);             }         });     }      // ----------------------------------------------------      world shape CustomListAdapter extends BaseAdapter {         somebody ArrayList<String> listData;         somebody LayoutInflater layoutInflater;          world CustomListAdapter(Context context, ArrayList<String> listData) {             this.listData = listData;             layoutInflater = LayoutInflater.from(context);         }          @Override         world int getCount() {             render listData.size();         }          @Override         world Object getItem(int position) {             render listData.get(position);         }          @Override         world long getItemId(int position) {             render position;         }          world View getView(int position, View convertView, ViewGroup parent) {             ViewHolder holder;             if (convertView == null) {                 convertView = layoutInflater.inflate(R.layout.row, null);                 holder = novel ViewHolder();                 holder.icon = (ImageView)convertView.findViewById(R.id.icon);                 holder.text = (TextView)convertView.findViewById(R.id.text);                 convertView.setTag(holder);             } else {                 holder = (ViewHolder) convertView.getTag();             }              holder.text.setText(                     String.valueOf(position) + "\n" + src[position]);              if (holder.icon != null) {                 novel BitmapWorkerTask(holder.icon).execute(listData.get(position));             }             render convertView;         }          shape ViewHolder {             ImageView icon;             TextView text;         }     }      // ----------------------------------------------------     // Load bitmap inward AsyncTask     // ref:     // http://developer.android.com/training/displaying-bitmaps/process-bitmap.html     shape BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {         somebody concluding WeakReference<ImageView> imageViewReference;         somebody String imageUrl;          world BitmapWorkerTask(ImageView imageView) {             // Use a WeakReference to ensure the ImageView tin hold upwards garbage             // collected             imageViewReference = novel WeakReference<ImageView>(imageView);         }          // Decode picture inward background.         @Override         protected Bitmap doInBackground(String... params) {             imageUrl = params[0];             render LoadImage(imageUrl);         }          // Once complete, meet if ImageView is however around in addition to prepare bitmap.         @Override         protected void onPostExecute(Bitmap bitmap) {             if (imageViewReference != nada && bitmap != null) {                 concluding ImageView imageView = imageViewReference.get();                 if (imageView != null) {                     imageView.setImageBitmap(bitmap);                 }             }         }          somebody Bitmap LoadImage(String URL) {             Bitmap bitmap = null;             InputStream inward = null;             assay {                 inward = OpenHttpConnection(URL);                 bitmap = BitmapFactory.decodeStream(in);                 in.close();             } grab (IOException e1) {             }             render bitmap;         }          somebody InputStream OpenHttpConnection(String strURL)                 throws IOException {             InputStream inputStream = null;             URL url = novel URL(strURL);             URLConnection conn = url.openConnection();              assay {                 HttpURLConnection httpConn = (HttpURLConnection) conn;                 httpConn.setRequestMethod("GET");                 httpConn.connect();                  if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {                     inputStream = httpConn.getInputStream();                 }             } grab (Exception ex) {             }             render inputStream;         }     } }  

layout/row.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="match_parent">      <ImageView         android:id="@+id/icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />      <TextView         android:id="@+id/text"         android:layout_width="match_parent"         android:layout_height="wrap_content" />  </LinearLayout> 

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidimage.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <ListView         android:id="@+id/imagelist"         android:layout_width="fill_parent"         android:layout_height="fill_parent" /> </LinearLayout>  

uses-permission of “android.permission.INTERNET” is needed inward AndroidManifest.xml

 WebP is a modern picture format that provides superior lossless in addition to lossy compression for i Load WebP from Internet in addition to display inward ListViewDownload the files .