android 按时间显示图片

xiaoxiao2021-02-28  142

实现效果:

首先写需要用到的图片数据:时间、文件名、路径等

package com.example.a550211.cd; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by xing on 2017/7/4. */ public class ImageTime { private long time; private String thumbPath; private String filePath; private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getDate() { return new SimpleDateFormat("yyyy年MM月dd日") .format(new Date(time*1000L)); } public long getTime() { return time; } public void setTime(long time) { this.time = time; } public String getThumbPath() { return thumbPath; } public void setThumbPath(String thumbPath) { this.thumbPath = thumbPath; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } } 2、实现手机图片的获取,这里使用LoaderCallbacks<Cursor>去获取手机图片,然后保存到date数据中

package com.example.a550211.cd; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader; import android.database.Cursor; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.trustyapp.gridheaders.TrustyGridGridView; import java.io.File; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private TrustyGridGridView gvImage; private ImageAdapter adapter; private ArrayList<ImageTime> fileInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { gvImage = (TrustyGridGridView)findViewById(R.id.gv_image); adapter = new ImageAdapter(this,fileInfo); gvImage.setAdapter(adapter); fileInfo = new ArrayList<>(); getLoaderManager().initLoader(1, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] STORE_IMAGES = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Thumbnails.DATA }; CursorLoader cursorLoader = new CursorLoader( this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STORE_IMAGES, null, null, null); return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if (cursor.moveToNext()){ int thumbPathIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA); int timeIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED); int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); do{ ImageTime fi = new ImageTime(); String thumbPath = cursor.getString(thumbPathIndex); Long date = cursor.getLong(timeIndex); String filepath = cursor.getString(pathIndex); File f = new File(filepath); fi.setTime(date); fi.setThumbPath(thumbPath); fi.setFilePath(filepath); fi.setFileName(f.getName()); fileInfo.add(fi); }while (cursor.moveToNext()); } adapter.setData(fileInfo); adapter.notifyDataSetChanged(); } @Override public void onLoaderReset(Loader<Cursor> loader) { } } 3、adapter中显示数据实现,这里使用到了TrustyGridSimpleAdapter,这里面有两个方法getHeaderId和getHeaderView具体功能实现下面代码。

package com.example.a550211.cd; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.signature.MediaStoreSignature; import com.trustyapp.gridheaders.TrustyGridSimpleAdapter; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; /** * Created by xing on 2017/7/11. */ public class ImageAdapter extends BaseAdapter implements TrustyGridSimpleAdapter { private Context mContext; private ArrayList<ImageTime> fileInfo; public ImageAdapter(Context mContext,ArrayList<ImageTime> fileInfo) { this.mContext = mContext; this.fileInfo =fileInfo; } public void setData(ArrayList<ImageTime> fileInfo){ this.fileInfo = fileInfo; } @Override public int getCount() { int count = 0; if (fileInfo!=null && fileInfo.size()>0){ count = fileInfo.size(); } return count; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } class ViewHolder{ ImageView ivImage; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView==null){ viewHolder = new ViewHolder(); convertView = View.inflate(mContext,R.layout.item_image,null); viewHolder.ivImage = (ImageView)convertView.findViewById(R.id.iv_image); convertView.setTag(viewHolder); }else { viewHolder = (ViewHolder)convertView.getTag(); } if (fileInfo!=null && fileInfo.size()>0) { File file = new File(fileInfo.get(position).getFilePath()); Glide.with(mContext).load(file).asBitmap().dontAnimate().centerCrop() .signature(new MediaStoreSignature("image/jpeg", file.lastModified(), 0)) .into(viewHolder.ivImage); } return convertView; } class HeaderViewHolder { public TextView tvTimeHeader; } @Override public View getHeaderView(int position, View convertView, ViewGroup viewGroup) { HeaderViewHolder mHeadViewHolder = null; if (convertView==null){ mHeadViewHolder = new HeaderViewHolder(); convertView = View.inflate(mContext,R.layout.item_time_header,null); mHeadViewHolder.tvTimeHeader = (TextView) convertView.findViewById(R.id.tv_time_header); convertView.setTag(mHeadViewHolder); }else { mHeadViewHolder = (HeaderViewHolder)convertView.getTag(); } mHeadViewHolder.tvTimeHeader.setText(fileInfo.get(position).getDate()); return convertView; } @Override public long getHeaderId(int i) { // File file = new File(fileInfo.get(i).getFilePath()); return getTimeId(fileInfo.get(i).getDate()); // return getTimeId(strToDateLong(file.lastModified())); } public long getTimeId(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date mDate = null; try { mDate = sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mDate.getTime(); } public String strToDateLong(long time) { Date date = new Date(time); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); return sdf.format(date); } }实现的三个布局文件,都比较简单

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.a550211.cd.MainActivity"> <com.trustyapp.gridheaders.TrustyGridGridView android:id="@+id/gv_image" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="3dp" android:numColumns="4" android:padding="3dp" android:verticalSpacing="3dp"></com.trustyapp.gridheaders.TrustyGridGridView> </RelativeLayout> <?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/iv_image" android:layout_width="104dp" android:layout_height="104dp" android:layout_centerInParent="true" android:scaleType="fitXY" /> </LinearLayout> <?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"> <TextView android:id="@+id/tv_time_header" android:layout_width="wrap_content" android:layout_height="34dp" android:layout_marginLeft="17dp" android:text="fdsfsdfsdf" android:gravity="center" android:textColor="#000000" android:textSize="15sp" /> </LinearLayout> 需要使用到的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />最后记得添加jar包,下载路径: http://download.csdn.net/download/u011324501/9895566

代码下载地址:http://download.csdn.net/download/u011324501/9895782

转载请注明原文地址: https://www.6miu.com/read-76467.html

最新回复(0)