本文介绍了显示画廊的图片在我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从它的数据库中检索库的所有图像,并希望在我的GridView的应用程序来显示。请告诉怎么做..
解决方案
进口java.io.BufferedInputStream中;
进口java.io.ByteArrayOutputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.drawable.BitmapDrawable;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.os.Environment;
进口android.provider.MediaStore;
进口android.util.Log;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;
进口android.widget.AdapterView.OnItemLongClickListener;
/ **
*类将显示所有画廊图像网格视图此类具有
*功能从公众席专用移动图像
*
* /
公共类AndroidGallery扩展活动实现OnItemLongClickListener {
私人的GridView sdcardImages;
私人ImageAdapter imageAdapter;
私人字符串变量= this.getClass()getSimpleName()。
私人的ArrayList< LoadedImage>照片=新的ArrayList< LoadedImage>();
私人字符串thumb_Image_Path;
私人字符串密码= NULL;
私人的FileInputStream是= NULL;
私人的BufferedInputStream双= NULL;
私人位图位图;
私人ByteArrayOutputStream字节;
私人文件galleryThumbFile,galleryImageFile,galleryThumbFolder,galleryImageFolder;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.sdcard);
}
@覆盖
保护无效onResume(){
// TODO自动生成方法存根
super.onResume();
photos.clear(); //如果照片数组列表有任何previous值,然后将其清除
setViews(); //设置意见
setProgressBarIndeterminateVisibility(真正的);
sdcardImages.setAdapter(imageAdapter); //填充适配器
loadImages();从默认的库//加载图片
}
/ **
*免费了位图相关的资源。
* /
保护无效的onDestroy(){
super.onDestroy();
最终的GridView网格= sdcardImages;
最终诠释计数= grid.getChildCount();
ImageView的V = NULL;
的for(int i = 0; I<计数;我++){
V =(ImageView的)grid.getChildAt(ⅰ);
((BitmapDrawable)v.getDrawable())setCallback(空)。
}
}
/ **
*载入图片...
* /
私人无效loadImages(){
最终目标数据= getLastNonConfigurationInstance();
如果(数据== NULL){
新LoadImagesFromSDCard()执行()。
} 其他 {
最后LoadedImage []照片=(LoadedImage [])的数据;
如果(photos.length == 0){
新LoadImagesFromSDCard()执行()。
}
对于(LoadedImage照片:照片){
addImage(照片);
}
}
}
/ **
*从SD卡加载图像的背景,并显示在每个图像
* 屏幕。
*
* @see android.os.AsyncTask#doInBackground(PARAMS [])
* /
私有类LoadImagesFromSDCard扩展
AsyncTask的<对象,LoadedImage,对象> {
@覆盖
保护对象doInBackground(对象... PARAMS){
开放的我们的uri = NULL;
点阵位图= NULL;
位图newBitmap = NULL;
的String []投影= {MediaStore.Images.Thumbnails._ID};
光标光标= managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
投影,//哪些列返回
空,//返回所有行
空值,
空值);
INT参数:columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
INT大小= cursor.getCount();
INT imageID = 0;
的for(int i = 0; I<大小;我++){
cursor.moveToPosition(ⅰ);
imageID = cursor.getInt(参数:columnIndex);
的uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,+ imageID);
尝试 {
位= BitmapFactory.de codeStream(getContentResolver()openInputStream(URI));
如果(位图!= NULL){
newBitmap = Bitmap.createScaledBitmap(位图,70%,70,真);
bitmap.recycle();
如果(newBitmap!= NULL){
publishProgress(新LoadedImage(newBitmap,将String.valueOf(imageID)));
}
}
}赶上(IOException异常E){
//错误获取图像,尝试恢复
Log.e(TAG,e.toString());
}
}
返回null;
}
/ **
*添加新的LoadedImage图像中的网格。
*
*参数值
* 图片。
* /
@覆盖
公共无效onProgressUpdate(LoadedImage ...值){
addImage(值);
}
/ **
*设置进度条的可见性为false。
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object中)
* /
@覆盖
保护无效onPostExecute(对象结果){
setProgressBarIndeterminateVisibility(假);
}
}
/ **
*添加图像(S),以网格视图适配器。
*
*参数值
*的LoadedImages引用数组
* /
私人无效addImage(LoadedImage ...值){
对于(LoadedImage图片:值){
imageAdapter.addPhoto(图像);
imageAdapter.notifyDataSetChanged();
}
}
/ **
*设置浏览功能将设置意见
*
* /
私人无效setViews(){
sdcardImages =(GridView控件)findViewById(R.id.sdcard);
sdcardImages.setOnItemLongClickListener(本); //设置监听器上的GridView
imageAdapter =新ImageAdapter(getApplicationContext());
imageAdapter.notifyDataSetChanged();
}
/ **
*函数将返回画廊大图位图
* @参数的ImagePath
* @返回
* /
私人位图getImageBitmap(字符串的ImagePath){
// TODO自动生成方法存根
尝试 {
是=新的FileInputStream(新文件(的ImagePath));
双=新的BufferedInputStream(是);
位= BitmapFactory.de codeStream(之二);
}赶上(例外五){
//尝试恢复
} 最后 {
尝试 {
如果(之二!= NULL){
bis.close();
}
如果(是!= NULL){
is.close();
}
}赶上(例外五){
}
}
System.gc()的;
返回的位图;
}
/ ** *函数将返回从图库中选择图片的拇指 * @参数thumbImagePath * @返回 * /
私人位图getThumbnailBitmap(字符串thumbImagePath){
尝试 {
是=新的FileInputStream(新文件(thumbImagePath));
双=新的BufferedInputStream(是);
位= BitmapFactory.de codeStream(之二);
}赶上(例外五){
//尝试恢复
} 最后 {
尝试 {
如果(之二!= NULL){
bis.close();
}
如果(是!= NULL){
is.close();
}
}赶上(例外五){
}
}
System.gc()的;
返回的位图;
}
}
/ **
*这个类是用于图像适配器
* @作者
*
* /
类ImageAdapter扩展了BaseAdapter {
私人语境mContext;
ImageAdapter(上下文的背景下){
mContext =背景;
}
公共无效addPhoto(LoadedImage照片){
photos.add(照片);
}
公众诠释getCount将(){
返回photos.size();
}
公共对象的getItem(INT位置){
返回photos.get(位置);
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
最后的ImageView ImageView的;
如果(convertView == NULL){
ImageView的=新ImageView的(mContext);
imageView.setLayoutParams(新GridView.LayoutParams(90,90));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} 其他 {
ImageView的=(ImageView的)convertView;
}
imageView.setImageBitmap(photos.get(位置).getImage());
返回ImageView的;
}
}
}
布局:
< XML版本=1.0编码=UTF-8&GT?;
< GridView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / SD卡
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT机器人:stretchMode =columnWidth中
机器人:重力=中心的Android版本:layout_below =@ ID / RelativeWallTopBar
机器人:为numColumns =auto_fit机器人:columnWidth中=90dp
机器人:horizontalSpacing =5dip机器人:verticalSpacing =15dip/>
I want to retrieve all the images from gallery from its database and want to display in my application in gridview.please tell how to do it..
解决方案
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemLongClickListener;
/**
* Class will Display all gallery images in grid view This class has the
* functionality to move images from public gallery to Private
*
*/
public class AndroidGallery extends Activity implements OnItemLongClickListener {
private GridView sdcardImages;
private ImageAdapter imageAdapter;
private String TAG = this.getClass().getSimpleName();
private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();
private String thumb_Image_Path;
private String crypto = null;
private FileInputStream is = null;
private BufferedInputStream bis = null;
private Bitmap bitmap;
private ByteArrayOutputStream bytes;
private File galleryThumbFile, galleryImageFile,galleryThumbFolder, galleryImageFolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
photos.clear(); // If photos array list has any previous value then clear it
setViews(); // set views
setProgressBarIndeterminateVisibility(true);
sdcardImages.setAdapter(imageAdapter); // Fill adapter
loadImages(); // Load images from default gallery
}
/**
* Free up bitmap related resources.
*/
protected void onDestroy() {
super.onDestroy();
final GridView grid = sdcardImages;
final int count = grid.getChildCount();
ImageView v = null;
for (int i = 0; i < count; i++) {
v = (ImageView) grid.getChildAt(i);
((BitmapDrawable) v.getDrawable()).setCallback(null);
}
}
/**
* Load images...
*/
private void loadImages() {
final Object data = getLastNonConfigurationInstance();
if (data == null) {
new LoadImagesFromSDCard().execute();
} else {
final LoadedImage[] photos = (LoadedImage[]) data;
if (photos.length == 0) {
new LoadImagesFromSDCard().execute();
}
for (LoadedImage photo : photos) {
addImage(photo);
}
}
}
/**
* Load images from SD Card in the background, and display each image on the
* screen.
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
private class LoadImagesFromSDCard extends
AsyncTask<Object, LoadedImage, Object> {
@Override
protected Object doInBackground(Object... params) {
Uri uri = null;
Bitmap bitmap = null;
Bitmap newBitmap = null;
String[] projection = {MediaStore.Images.Thumbnails._ID};
Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap,String.valueOf(imageID)));
}
}
} catch (IOException e) {
//Error fetching image, try to recover
Log.e(TAG, e.toString());
}
}
return null;
}
/**
* Add a new LoadedImage in the images grid.
*
* @param value
* The image.
*/
@Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
}
/**
* Set the visibility of the progress bar to false.
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}
/**
* Add image(s) to the grid view adapter.
*
* @param value
* Array of LoadedImages references
*/
private void addImage(LoadedImage... value) {
for (LoadedImage image : value) {
imageAdapter.addPhoto(image);
imageAdapter.notifyDataSetChanged();
}
}
/**
* Set view function will set views
*
*/
private void setViews() {
sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setOnItemLongClickListener(this); // Set Listener on GridView
imageAdapter = new ImageAdapter(getApplicationContext());
imageAdapter.notifyDataSetChanged();
}
/**
* Function will return bitmap of Gallery big image
* @param imagePath
* @return
*/
private Bitmap getImageBitmap(String imagePath) {
// TODO Auto-generated method stub
try {
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
} catch (Exception e) {
// Try to recover
} finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
System.gc();
return bitmap;
}
/** * Function will return thumb image of selected from gallery * @param thumbImagePath * @return */
private Bitmap getThumbnailBitmap(String thumbImagePath) {
try {
is = new FileInputStream(new File(thumbImagePath));
bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
} catch (Exception e) {
// Try to recover
} finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
System.gc();
return bitmap;
}
}
/**
* This class is used for image adapter
* @author
*
*/
class ImageAdapter extends BaseAdapter {
private Context mContext;
ImageAdapter(Context context) {
mContext = context;
}
public void addPhoto(LoadedImage photo) {
photos.add(photo);
}
public int getCount() {
return photos.size();
}
public Object getItem(int position) {
return photos.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(photos.get(position).getImage());
return imageView;
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sdcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:stretchMode="columnWidth"
android:gravity="center" android:layout_below="@id/RelativeWallTopBar"
android:numColumns="auto_fit" android:columnWidth="90dp"
android:horizontalSpacing="5dip" android:verticalSpacing="15dip" />
这篇关于显示画廊的图片在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!