Android 实现类似于QQ空间选择并展示本地图片
新人刚刚接触android,最近由于项目需求,需要实现类似于QQ空间里的发说说功能,大家都知道说说能发本地图片,对于刚刚入坑的新人自己实现还是花点时间的,于是上网查了下参考了许多网上大神的思路,当然实战中还是碰到了很多问题,自己做了些修改,终于做出了一个基本功能的版本。放在这里权当做个笔记,这里只实现了最简单的功能,要用到实际还需要添加很多东西哈,这里只做了读取并展示,上传以后再加。这里都是自己新入安卓的探索过程,漏洞百出,不喜勿喷哈~
先放个效果图吧
选择前 选择后好了下面记录自己的思路。
首先需要实现图片的展示,这里用Gridview实现,图片写死了每行放四张。。初始demo以后再优化。防止图片选择过多无限往下占用屏幕影响体验,Gridview最多放20张图片,显示两行。下面是activity里Gridview的布局
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
/>
然后再activity代码里设置gridview的大小(xml里wrap_content实际只显示一行,最简单的思路是调整高度实现显示行数)
//设置gridview跟屏幕等宽,高度为两个图片的长度,这样就显示两行
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int widthSingle = (width-50)/4;
ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(width, widthSingle*2 + 20);
gridview.setLayoutParams(lp);
先写Gridview的adapter
public class ItemAdapter extends BaseAdapter {
private List<String> list = new ArrayList<String>();//图片url列表
private Context context;
private static int MAX_SINGLE_LINE = 4;//每行显示4张图片
public ItemAdapter (List<String> list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.layout_image_item, null);
viewholder = new ViewHolder();
viewholder.iamge = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
//设置图片长宽为屏幕宽度四分之一
WindowManager wm = (WindowManager) ActivityUtil.findActivity(context).getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int widthSingle = (width-MAX_SINGLE_LINE;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(widthSingle, widthSingle);
lp.setMargins(5, 0, 0, 0);
if (list.get(position).equals("default")) { //添加默认图片
viewholder.iamge.setLayoutParams(lp);
viewholder.iamge.setImageResource(R.mipmap.add_photo);
}
else {
Bitmap loacalBitmap = LoadUrl(list.get(position));
if (loacalBitmap != null) {
viewholder.iamge.setLayoutParams(lp);
viewholder.iamge.setImageBitmap(loacalBitmap);
}
}
return convertView;
}
//读取图片url
public static Bitmap LoadUrl(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
class ViewHolder {
ImageView iamge;
}
}
下面是activity里Gridview的布局
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
/>
然后再activity代码里设置gridview,findViewById就省略了
private GridView gridview;
private ItemAdapter itemAdapter;
private List<String> list = new ArrayList<String>();//存储选中图片的url
private final int MAX_PHOTO = 21;//最多20张图
private static final int MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1; private static final int MY_PERMISSIONS_READ_EXTERNAL_STORAGE = 2;
onCreate里
// xml里设置wrap_content实际只显示一行,最简单的思路是调整高度实现显示行数
//设置gridview跟屏幕等宽,高度为两个图片的长度,这样就显示两行
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int widthSingle = (width-50)/4;
ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(width, widthSingle*2 + 20);
gridview.setLayoutParams(lp);
//添加默认图片
list.add("default");
itemAdapter = new ItemAdapter(list,context);
gridview.setAdapter(photoAdapter);
然后设置监听,点击默认图片进入相册选择图片,进入相册获取图片url的方法:
/**
* 进入相册
*/
private void JumpToDCIM(){
Intent intent =new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
选择图片后的回调,获取url
/**
* 进入相册后选择图片的回调
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data == null){
return;
}
if (requestCode == 0) {
try {
Uri originalUri = data.getData();
ContentResolver testcr = getContentResolver();
Cursor cursor = testcr.query(originalUri, new String[] { MediaStore.Images.Media.DATA }, null, null, null);
if(infoList.size() == MAX_PHOTO){
removeItem();
refreshAdapter();
return;
}
if (null == cur) {
return;
}
removeItem();
for ( cursor.moveToFirst(); ! cursor.isAfterLast(); cursor.moveToNext()) {
int dataColumn = curso.getColumnIndex(MediaStore.Images.Media.DATA);
String image_path = cursor.getString(dataColumn);
infoList.add(image_path);
}
infoList.add("default");
refreshAdapter();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
refreshAdapter方法:
/**
* 刷新视图
*/
private void refreshAdapter(){
if(infoList == null){
infoList = new ArrayList<String>();
}
if(photoAdapter == null){
photoAdapter = new PhotoAdapter(infoList,mContext);
}
if(infoList.size() == MAX_PIC){
infoList.remove(infoList.size() - 1);
}
photoAdapter.notifyDataSetChanged();
}
removeItem方法:
/**
* 达到最大图片数删除默认图片,不能添加
*/
private void removeItem() {
if(infoList.size() -1 !=MAX_PIC){
if(infoList.size() !=0){//删除默认图片
infoList.remove(infoList.size() -1);
}
}
}
好了这样获取相册里图片url的方法写好了,我们在Gridview的监听里判断只要点击的是默认图片就调用这个JumpToDCIM方法。
然后发现并不好使,选择完图片后啥也没有增加。。。。。。
好吧继续调试,打断点试试,发现JumpToDCIM确实获取到了图片的url,问题出现在adapter的LoadUrl(String url)方法里,读取url时报了permission denied异常。上网查了下这是什么东东,发现是没有读取权限,好吧那就添加权限。
我们在进入相册之前先设置读写权限,在AndroidManifest.xml里添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
实战中发现Android6.0以后AndroidManifest.xml里的设置并不管用,坑。。。那就写个方法申请权限好了。
先写检查是否有权限的方法:
public static boolean checkPermission(Context context, String permissionStr){
boolean result = true;
if(Build.VERSION.SDK_INT >= 23){
result = (context.checkSelfPermission(permissionStr) == PackageManager.PERMISSION_GRANTED);
}
else{
result = (PermissionChecker.checkSelfPermission(context,permissionStr) == PackageManager.PERMISSION_GRANTED);
}
return result;
}
然后是请求权限的方法,调用该方法后系统会弹出弹窗确认:
@RequiresApi(api = 23)
public static void requestPermission(Activity activity, String[] permissionStrs, int requestCode){
activity.requestPermissions(permissionStrs,requestCode);
}
请求权限的回调,这里需要开头对permissions,grantResults进行非空判断,不然请求权限的弹窗出现时在用户选择之前就会回调,permissions,grantResults此时都为空就会崩溃。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
if( permissions.length > 0 && grantResults.length > 0
if (requestCode == MY_PERMISSIONS_READ_EXTERNAL_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
JumpToDCIM();
} else {
// Permission Denied
Toast.makeText(mContext, "用户拒绝添加读取权限", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
JumpToDCIM ();
} else {
// Permission Denied
Toast.makeText(mContext, "用户拒绝添加写入权限", Toast.LENGTH_SHORT).show();
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
好了现在可以在Gridview里添加监听读取相册了,代码如下。
/**
* 如果是默认图片则点击进入相册添加,进入前判断是否有读写权限,没有的话申请权限(Android 6.0/API 23以上时AndroidManifest里添加权限无效需要申请权限)
*/
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(infoList.get(position).equals("default")){
if(checkPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE) && checkPermission(mContext,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
JumpToDCIM ();
}
else {
if(Build.VERSION.SDK_INT >= 23) {
requestPermission(AddDiaryActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_READ_EXTERNAL_STORAGE);
requestPermission(AddDiaryActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
}
}
}
}
});
OK,一个最最简单功能的demo就实现了,收工。