标签:des android cWeb style blog http io ar color
https://github.com/nostra13/Android-Universal-Image-Loader
A simple example to show all the pictures in the SD card in a gridView.
<1>Features
<2>Example Screenshot
<3>Example Code
Copy the jar to the libs subfolder in the project
Structure of the project
More interrelation of the classes
Code
//AndroidManifest.xml
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="mirror.android.universalimageloadertest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="mirror.android.universalimageloadertest.GridViewActivity" android:label="GridView"> </activity> <activity android:name="mirror.android.universalimageloadertest.ShowClickedImage" android:label="ShowImage" android:theme="@android:style/Theme.Holo.Dialog"> </activity> </application>
//MainActivity.class package mirror.android.universalimageloadertest; import com.example.universalimageloadertest.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void ShowGridView(View view){ Intent intent = new Intent(MainActivity.this,GridViewActivity.class); startActivity(intent); } }
//activity_main.xml <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" tools:context="mirror.android.universalimageloadertest.MainActivity" > <Button android:id="@+id/bt_gridView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="ShowGridView" android:text="GridView" /> </RelativeLayout>
//GridViewActivity.class package mirror.android.universalimageloadertest; import java.util.HashMap; import com.example.universalimageloadertest.R; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class GridViewActivity extends Activity{ private MyGridViewAdapter adpater; private HashMap<Integer, MyModel> mediaImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview_activity_layout); init(); } private void init(){ GridView gridView = (GridView)findViewById(R.id.gridView); mediaImage = ImageUtil.getMediaImage(this); //mediaImage is the returned "datas" adpater = new MyGridViewAdapter(this, mediaImage); //setAdapter---> Sets the data behind this GridView. gridView.setAdapter(adpater); //gridView.setOnItemClickListener(this); } /*@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mediaImage.put(position, new MyModel(mediaImage.get(position).path, false)); adpater.notifyDataSetChanged(); }*/ }
//gridview_activity_layout.xml <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:horizontalSpacing="4dip" android:numColumns="3" android:padding="4dip" android:stretchMode="columnWidth" android:verticalSpacing="4dip" />
//MyGridviewAdapter.class package mirror.android.universalimageloadertest; import java.util.HashMap; import java.util.zip.Inflater; import com.example.universalimageloadertest.R; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.sax.StartElementListener; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.ImageView; public class MyGridViewAdapter extends BaseAdapter { private static DisplayImageOptions options; private static ImageLoader imageLoader; private Context context; private HashMap<Integer, MyModel> datas; private ViewHolder holder; public MyGridViewAdapter(Context context,HashMap<Integer, MyModel> datas){ this.context = context; this.datas = datas; imageLoader = ImageLoader.getInstance(); imageLoader.init(ImageLoaderConfiguration.createDefault(context)); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher) .showImageForEmptyUri(R.drawable.ic_launcher) .showImageOnFail(R.drawable.ic_launcher) .cacheInMemory(true) .cacheOnDisk(true) .considerExifParams(true) //.bitmapConfig(Bitmap.Config.RGB_565) .build(); } @Override public int getCount() { //Log.d("MirrorActivity","datas.size()--->" + String.valueOf(datas.size())); return datas.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } //Get a View that displays the data at the specified position in the data set. //You can either create a View manually or inflate it from an XML layout file. @Override public View getView(final int position, View convertView, ViewGroup parent) { if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item_layout, parent, false); holder.imageView = (ImageView)convertView.findViewById(R.id.image); convertView.setTag(holder); } else holder = (ViewHolder)convertView.getTag(); //Log.d("Postion",String.valueOf(position)); //Log.d("datas(position)",datas.get(position).toString()); final String ImagePath = datas.get(position).path; imageLoader.displayImage(ImagePath, holder.imageView, options); holder.imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Log.d("MirrorImageClick","Click " + String.valueOf(position)); Intent intent = new Intent(context, ShowClickedImage.class); Bundle bundle = new Bundle(); bundle.putString("ImagePath", ImagePath); intent.putExtras(bundle); context.startActivity(intent); } }); //Log.d("sss","222"); return convertView; } private static class ViewHolder{ ImageView imageView; } }
//gridview_item_layout.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="120dip" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="128dip" android:adjustViewBounds="true" android:contentDescription="ImageView" android:scaleType="centerCrop"/>" </FrameLayout>
//MyModel package mirror.android.universalimageloadertest; public class MyModel{ public String path = null; public boolean isChecked = false; public MyModel(String s, boolean b){ this.path = s; this.isChecked = b; } }
//ImageUtil package mirror.android.universalimageloadertest; import java.util.HashMap; import android.content.Context; import android.database.Cursor; import android.provider.MediaStore; import android.util.Log; public class ImageUtil { public static HashMap<Integer, MyModel> getMediaImage(Context context){ HashMap<Integer, MyModel> datas = new HashMap<Integer, MyModel>(); final String orderBy = MediaStore.Images.Media.DATE_TAKEN; final String[] colums = {MediaStore.Images.Media.DATA, //The data stream for the file MediaStore.Images.Media._ID, //The unique ID for a row. MediaStore.Images.Media.BUCKET_DISPLAY_NAME}; Cursor imageCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , colums , null , null , orderBy); Log.d("MirrorActivity","imageCursor.getCount()--->" + String.valueOf(imageCursor.getCount())); for(int i = 0 ; i < imageCursor.getCount() ; i++){ imageCursor.moveToPosition(i); //gets the zero-based ColumnIndex for the given column name int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA); int dirColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); //get the value of the requested column from ColumnIndex as a String. String bucketName = imageCursor.getString(dirColumnIndex); Log.d("MirrorActivity","bucketName = " + bucketName); String filename = "mirrorImage" + imageCursor.getString(dataColumnIndex); Log.d("MirrorActivity","filename = " + filename); /*try{ File file = new File(filename); if(!file.exists()){ } }catch(Exception e){ continue; }*/ MyModel galleryModel = new MyModel("file:/" + imageCursor.getString(dataColumnIndex).toString(), false); datas.put(i, galleryModel); //Log.d("MirrorActivityddd","datas:" + datas.toString()); } imageCursor.close(); //Log.d("MirrorActivity","datas:" + datas.toString()); //Log.d("MirrorActivity","datas.size()--->" + String.valueOf(datas.size())); return datas; } }
//ShowClickedImage.xml package mirror.android.universalimageloadertest; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import com.example.universalimageloadertest.R; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; //in the Menifest: android:theme="@android:style/Theme.Holo.Dialog" //to show this activity in a dialog public class ShowClickedImage extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_clicked_image); ImageView imageView = (ImageView)findViewById(R.id.showImageView); //Cannot use setImageURI(), cause //Andorid ImageURI can just resolve resource on the mobile, not including SD card and networks. //Solution: Read the file through FileInputStream and get Bitmap object Bundle bundle = getIntent().getExtras(); String path = bundle.getString("ImagePath"); //Log.d("Path",path); path = path.substring(6); // to delete the starts "file:/" //Log.d("Path",path); FileInputStream fis; try { fis = new FileInputStream(path); BufferedInputStream bis = new BufferedInputStream(fis); Bitmap bitmap = BitmapFactory.decodeStream(bis); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
//show_cliked_image.xml <?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" android:orientation="vertical" > <ImageView android:id="@+id/showImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:contentDescription="ImageViewDialog"/> </LinearLayout>
Android-Universal-Image-Loader
标签:des android cWeb style blog http io ar color
原文地址:http://www.cnblogs.com/iMirror/p/4112979.html