忽然覺得簡書不適合分享代碼 看著很亂 所以我上博客分享了我的代碼
首先記錄一下寫的比較好的二個
這個是調(diào)用PictureSelector?依賴來實現(xiàn)的?PictureSelector 主要是幫助多圖選取的(簡單)http://www.reibang.com/p/48fd284b70a6
這個完全是自己實現(xiàn)的(困難)?http://www.reibang.com/p/fd61fce8a3ef
這個不知道好不好用https://www.cnblogs.com/zhangqie/p/6709179.html
https://cloud.tencent.com/developer/article/1331025
https://blog.csdn.net/qq_36416136/article/details/71552681
自己實現(xiàn)
主布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:background="@color/color_white">
<include
? ? ? ? android:id="@+id/include"
? ? ? ? layout="@layout/titles" />
<Button
? ? ? ? android:id="@+id/tv_btn_logout"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="54dp"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginLeft="22dp"
? ? ? ? android:layout_marginTop="10dp"
? ? ? ? android:layout_marginRight="20dp"
? ? ? ? android:layout_marginBottom="5dp"
? ? ? ? android:background="@drawable/btn_background"
? ? ? ? android:text="提交"
? ? ? ? android:textColor="@color/color_white"
? ? ? ? android:textSize="18sp" />
<ScrollView
? ? ? ? android:id="@+id/scroview"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_above="@+id/tv_btn_logout"
? ? ? ? android:layout_below="@+id/include"
? ? ? ? android:orientation="vertical"
? ? ? ? android:scrollbars="none">
<LinearLayout
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:background="@color/color_white"
? ? ? ? ? ? android:orientation="horizontal">
<RelativeLayout
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="wrap_content">
<TextView
? ? ? ? ? ? ? ? ? ? android:id="@+id/tv_phone"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="28dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="10dp"
? ? ? ? ? ? ? ? ? ? android:text="您的手機號踩窖、QQ號微饥、郵箱是?"
? ? ? ? ? ? ? ? ? ? android:textColor="@color/black"
? ? ? ? ? ? ? ? ? ? android:textSize="18sp" />
<TextView
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="12dp"
? ? ? ? ? ? ? ? ? ? android:layout_toRightOf="@id/tv_phone"
? ? ? ? ? ? ? ? ? ? android:text="(選填)"
? ? ? ? ? ? ? ? ? ? android:textSize="15sp" />
<EditText
? ? ? ? ? ? ? ? ? ? android:id="@+id/ed_fj"
? ? ? ? ? ? ? ? ? ? android:layout_width="500dp"
? ? ? ? ? ? ? ? ? ? android:layout_height="50dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="15dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="50dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginRight="15dp"
? ? ? ? ? ? ? ? ? ? android:background="@drawable/under_line_transparent"
? ? ? ? ? ? ? ? ? ? android:hint="填寫多種聯(lián)系方式時,請用逗號分隔肄满。"
? ? ? ? ? ? ? ? ? ? android:imeOptions="actionDone"
? ? ? ? ? ? ? ? ? ? android:inputType="number"
? ? ? ? ? ? ? ? ? ? android:paddingLeft="20dp"
? ? ? ? ? ? ? ? ? ? android:textSize="14sp" />
<TextView
? ? ? ? ? ? ? ? ? ? android:id="@+id/tv_question"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/ed_fj"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="30dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="30dp"
? ? ? ? ? ? ? ? ? ? android:text="問題/意見描述"
? ? ? ? ? ? ? ? ? ? android:textColor="@color/black"
? ? ? ? ? ? ? ? ? ? android:textSize="17sp" />
<TextView
? ? ? ? ? ? ? ? ? ? android:id="@+id/tv_required"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/ed_fj"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="5dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="32dp"
? ? ? ? ? ? ? ? ? ? android:layout_toRightOf="@id/tv_question"
? ? ? ? ? ? ? ? ? ? android:text="(必填)"
? ? ? ? ? ? ? ? ? ? android:textSize="14sp" />
<RelativeLayout
? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/ed_fj"
? ? ? ? ? ? ? ? ? ? android:orientation="horizontal">
<TextView
? ? ? ? ? ? ? ? ? ? ? ? android:id="@+id/et_xz"
? ? ? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? ? ? ? ? ? ? android:layout_marginTop="30dp"
? ? ? ? ? ? ? ? ? ? ? ? android:layout_marginRight="20dp"
? ? ? ? ? ? ? ? ? ? ? ? android:text="(0/300)"
? ? ? ? ? ? ? ? ? ? ? ? android:textSize="14sp" />
</RelativeLayout>
<EditText
? ? ? ? ? ? ? ? ? ? android:id="@+id/ed_idea"
? ? ? ? ? ? ? ? ? ? android:layout_width="500dp"
? ? ? ? ? ? ? ? ? ? android:layout_height="184dp"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/tv_question"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="15dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="13dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginRight="15dp"
? ? ? ? ? ? ? ? ? ? android:background="@drawable/under_line_transparent"
? ? ? ? ? ? ? ? ? ? android:hint="請留下您寶貴的意見,字數(shù)不少于10個字"
? ? ? ? ? ? ? ? ? ? android:imeOptions="actionDone"
? ? ? ? ? ? ? ? ? ? android:maxLength="300"
? ? ? ? ? ? ? ? ? ? android:paddingLeft="20dp"
? ? ? ? ? ? ? ? ? ? android:paddingBottom="140dp"
? ? ? ? ? ? ? ? ? ? android:textSize="14sp" />
<TextView
? ? ? ? ? ? ? ? ? ? android:id="@+id/tv_sc"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/ed_idea"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="17dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="20dp"
? ? ? ? ? ? ? ? ? ? android:text="上傳圖片"
? ? ? ? ? ? ? ? ? ? android:textColor="@color/black"
? ? ? ? ? ? ? ? ? ? android:textSize="18sp" />
<TextView
? ? ? ? ? ? ? ? ? ? android:id="@+id/button_get_photo"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/ed_idea"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="90dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="23dp"
? ? ? ? ? ? ? ? ? ? android:text="(選填,您可以上傳3張)"
? ? ? ? ? ? ? ? ? ? android:textSize="14sp" />
<android.support.v7.widget.RecyclerView
? ? ? ? ? ? ? ? ? ? android:id="@+id/recyclerview"
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_below="@id/tv_sc"
? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginTop="15dp"
? ? ? ? ? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? ? ? ? ? android:overScrollMode="never" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
recyclerview? ?item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="wrap_content"
? ? android:layout_height="wrap_content">
<RelativeLayout
? ? ? ? android:layout_width="85dp"
? ? ? ? android:layout_height="85dp"
? ? ? ? android:layout_centerInParent="true">
<ImageView
? ? ? ? ? ? android:id="@+id/fiv"
? ? ? ? ? ? android:layout_width="80dp"
? ? ? ? ? ? android:layout_height="80dp"
? ? ? ? ? ? android:layout_marginTop="10dp"
? ? ? ? ? ? android:contentDescription="@null"
? ? ? ? ? ? android:scaleType="centerCrop"
? ? ? ? ? ? android:src="@color/cus_view_detail_lines" />
<LinearLayout
? ? ? ? ? ? android:id="@+id/ll_del"
? ? ? ? ? ? android:layout_width="30dp"
? ? ? ? ? ? android:layout_height="30dp"
? ? ? ? ? ? android:layout_alignParentEnd="true"
? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? android:gravity="end"
? ? ? ? ? ? android:orientation="vertical">
<ImageView
? ? ? ? ? ? ? ? android:id="@+id/iv_del"
? ? ? ? ? ? ? ? android:layout_width="25dp"
? ? ? ? ? ? ? ? android:layout_height="25dp"
? ? ? ? ? ? ? ? android:contentDescription="@null"
? ? ? ? ? ? ? ? android:scaleType="fitXY"
? ? ? ? ? ? ? ? android:src="@mipmap/red" />
</LinearLayout>
<ImageView
? ? ? ? ? ? android:id="@+id/iv_duration"
? ? ? ? ? ? android:layout_width="80dp"
? ? ? ? ? ? android:layout_height="80dp"
? ? ? ? ? ? android:scaleType="centerCrop" />
</RelativeLayout>
</RelativeLayout>
java代碼
集合工具類
public class Image {
private Bitmapphoto;
? ? public Image(Bitmap photo) {
this.photo = photo;
? ? }
public BitmapgetPhoto() {
return photo;
? ? }
public void setPhoto(Bitmap photo) {
this.photo = photo;
? ? }
}
recyclerview適配器
public class CommitsAdapterextends RecyclerView.Adapter {
//用于判斷是否顯示圖標
//有圖片
? ? public static final int TYPE_CAMERA =1;
? ? //沒有圖片
? ? public static final int TYPE_PICTURE =2;
? ? private LayoutInflatermInflater;
? ? private Contextcontext;
? ? private int selectMax =4;
? ? /**
* 點擊添加圖片跳轉(zhuǎn)
*/
? ? private onAddPicClickListenermOnAddPicClickListener;
? ? public interface onAddPicClickListener {
void onAddPicClick();
? ? }
//獲取最大顯示個數(shù)
? ? public void setSelectMax(int selectMax) {
this.selectMax = selectMax;
? ? }
//獲取圖片集合
this.list = list;
? ? }
public CommitsAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
this.context = context;
? ? ? ? mInflater = LayoutInflater.from(context);
? ? ? ? this.mOnAddPicClickListener = mOnAddPicClickListener;
? ? }
/**
* 創(chuàng)建ViewHolder
*/
? ? @NonNull
@Override
? ? public ViewHolderonCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view =mInflater.inflate(R.layout.photo_item,
? ? ? ? ? ? ? ? viewGroup, false);
? ? ? ? final ViewHolder viewHolder =new ViewHolder(view);
? ? ? ? return viewHolder;
? ? }
@Override
? ? public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
//少于selectMax張质涛,顯示繼續(xù)添加的圖標
? ? ? ? if (getItemViewType(position) ==TYPE_CAMERA) {
//? ? ? ? ? ? Image image = list.get(position);
? ? ? ? ? ? viewHolder.mImg.setImageResource(R.mipmap.group);
//? ? ? ? ? ? viewHolder.mImg.setImageBitmap(image.getPhoto());
? ? ? ? ? ? viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? ? ? public void onClick(View v) {
mOnAddPicClickListener.onAddPicClick();
? ? ? ? ? ? ? ? }
});
? ? ? ? ? ? viewHolder.ll_del.setVisibility(View.INVISIBLE);
? ? ? ? }else {
Image image =list.get(position);
//? ? ? ? ? ? viewHolder.mImg.setImageResource(R.mipmap.group);
? ? ? ? ? ? viewHolder.mImg.setImageBitmap(image.getPhoto());
? ? ? ? ? ? viewHolder.ll_del.setVisibility(View.VISIBLE);
? ? ? ? ? ? viewHolder.ll_del.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? ? ? public void onClick(View view) {
int index =viewHolder.getAdapterPosition();
? ? ? ? ? ? ? ? ? ? // 這里有時會返回-1造成數(shù)據(jù)下標越界,具體可參考getAdapterPosition()源碼,
// 通過源碼分析應(yīng)該是bindViewHolder()暫未繪制完成導(dǎo)致掰担,知道原因的也可聯(lián)系我~感謝
? ? ? ? ? ? ? ? ? ? if (index != RecyclerView.NO_POSITION) {
list.remove(index);
? ? ? ? ? ? ? ? ? ? ? ? notifyItemRemoved(index);
? ? ? ? ? ? ? ? ? ? ? ? notifyItemRangeChanged(index, list.size());
//? ? ? ? ? ? ? ? ? ? ? ? DebugUtil.i("delete position:", index + "--->remove after:" + list.size());
? ? ? ? ? ? ? ? ? ? }
}
});
? ? ? ? ? ? //itemView 的點擊事件
? ? ? ? ? ? if (mItemClickListener !=null) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? ? ? ? ? public void onClick(View v) {
int adapterPosition =viewHolder.getAdapterPosition();
? ? ? ? ? ? ? ? ? ? ? ? mItemClickListener.onItemClick(adapterPosition, v);
? ? ? ? ? ? ? ? ? ? }
});
? ? ? ? ? ? }
}
}
//點擊事件接口
? ? protected OnItemClickListenermItemClickListener;
? ? public interface OnItemClickListener {
void onItemClick(int position, View v);
? ? }
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
? ? }
@Override
? ? public int getItemCount() {
//判斷存儲圖片個數(shù)和最大個數(shù)
? ? ? ? if (list.size()
return list.size() +1;
? ? ? ? }else {
return list.size();
? ? ? ? }
}
//形式:條件表達式 汇陆?程序塊1 :程序塊2 ;
//含義:如果條件表達式為真带饱,則本程序語句執(zhí)行程序塊1毡代,程序塊1的值即為整個表達式的值, 否則為表達式2的值勺疼。
? ? private boolean isShowAddItem(int position) {
int size =list.size() ==0 ?0 :list.size();
? ? ? ? return position == size;
? ? }
@Override
? ? public int getItemViewType(int position) {
if (isShowAddItem(position)) {
//有圖片
? ? ? ? ? ? return TYPE_CAMERA;
? ? ? ? }else {
//沒有圖片
? ? ? ? ? ? return TYPE_PICTURE;
? ? ? ? }
}
public class ViewHolderextends RecyclerView.ViewHolder {
ImageViewmImg;
? ? ? ? LinearLayoutll_del;
? ? ? ? ImageViewiv_duration;
? ? ? ? public ViewHolder(@NonNull View view) {
super(view);
? ? ? ? ? ? mImg = (ImageView) view.findViewById(R.id.fiv);
? ? ? ? ? ? ll_del = (LinearLayout) view.findViewById(R.id.ll_del);
? ? ? ? ? ? iv_duration = (ImageView) view.findViewById(R.id.iv_duration);
? ? ? ? }
}
}
主類
public class CommitsActivityextends Activity {
public static final int CHOOSE_PHOTO =2;// 相冊
? ? private RecyclerViewrecyclerview;
? ? private int maxSelectNum =3;//最多到幾張還可以選擇
? ? private CommitsAdapteradapter;
? ? @Override
? ? protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.feedback_commits);
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setinit();
? ? ? ? setonclick();
? ? }
private void setinit() {
recyclerview = findViewById(R.id.recyclerview); //設(shè)置布局管理器
//? ? GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
? ? ? ? GridLayoutManager layoutManager =new GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL,false);
? ? ? ? recyclerview.setLayoutManager(layoutManager);
? ? ? ? //設(shè)置adapter
? ? ? ? adapter =new CommitsAdapter(this,onAddPicClickListener);
? ? ? ? adapter.setList(selectList);
? ? ? ? adapter.setSelectMax(maxSelectNum);
? ? ? ? recyclerview.setAdapter(adapter);
? ? }
private void setonclick() {
}
private CommitsAdapter.onAddPicClickListeneronAddPicClickListener =new CommitsAdapter.onAddPicClickListener() {
@Override
? ? ? ? public void onAddPicClick() {
show();
? ? ? ? }
};
? ? private void show() {
final Dialog dialog =new Dialog(this, R.style.ActionSheetDialogStyle);
? ? ? ? View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_item, null);
? ? ? ? //相冊
? ? ? ? Button bt_camera = inflate.findViewById(R.id.bt_camera);
? ? ? ? //取消
? ? ? ? Button bt_cancel = inflate.findViewById(R.id.bt_cancel);
? ? ? ? dialog.setContentView(inflate);
? ? ? ? Window dialogWindow = dialog.getWindow();
? ? ? ? dialogWindow.setGravity(Gravity.BOTTOM);
? ? ? ? WindowManager.LayoutParams lp = dialogWindow.getAttributes();
? ? ? ? lp.y =20;
? ? ? ? dialogWindow.setAttributes(lp);
? ? ? ? bt_cancel.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View view) {
dialog.dismiss();
? ? ? ? ? ? }
});
? ? ? ? bt_camera.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View view) {
dialog.dismiss();
? ? ? ? ? ? ? ? if (ContextCompat.checkSelfPermission(CommitsActivity.this,
? ? ? ? ? ? ? ? ? ? ? ? android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(CommitsActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
? ? ? ? ? ? ? ? }else {
//從相冊獲取圖片
? ? ? ? ? ? ? ? ? ? openAlbum();
? ? ? ? ? ? ? ? }
}
});
? ? ? ? dialog.show();
? ? }
private void openAlbum() {
Intent intent =new Intent("android.intent.action.GET_CONTENT");
? ? ? ? intent.setType("image/*");
? ? ? ? startActivityForResult(intent, CHOOSE_PHOTO);//打開相冊
? ? }
//相冊
? ? @Override
? ? public void onRequestPermissionsResult(int requestCode, String[] permissions,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
? ? ? ? switch (requestCode) {
case 1:
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
? ? ? ? ? ? ? ? }else {
Toast.makeText(this, "你拒絕了權(quán)限申請教寂,可能無法打開相冊喲", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? default:
}
}
@Override
? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CHOOSE_PHOTO:
if (resultCode ==RESULT_OK) {
// 判斷手機系統(tǒng)版本號
? ? ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >=19) {
// 4.4及以上系統(tǒng)使用這個方法處理圖片
? ? ? ? ? ? ? ? ? ? ? ? handleImageOnKitKat(data);
? ? ? ? ? ? ? ? ? ? }else {
// 4.4以下系統(tǒng)使用這個方法處理圖片
? ? ? ? ? ? ? ? ? ? ? ? handleImageBeforeKitKat(data);
? ? ? ? ? ? ? ? ? ? }
}
break;
? ? ? ? ? ? default:
break;
? ? ? ? }
}
@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath =null;
? ? ? ? Uri uri = data.getData();
? ? ? ? if (DocumentsContract.isDocumentUri(this, uri)) {
// 如果是document類型的Uri,則通過document id處理
? ? ? ? ? ? String docId = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1]; // 解析出數(shù)字格式的id
? ? ? ? ? ? ? ? String selection = MediaStore.Images.Media._ID +"=" + id;
? ? ? ? ? ? ? ? imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
? ? ? ? ? ? }else if ("com.android.providers.downloads.documents".equals(uri.
getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content: downloads/public_downloads"), Long.valueOf(docId));
? ? ? ? ? ? ? ? imagePath = getImagePath(contentUri, null);
? ? ? ? ? ? }
}else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果是content類型的Uri执庐,則使用普通方式處理
? ? ? ? ? ? imagePath = getImagePath(uri, null);
? ? ? ? }else if ("file".equalsIgnoreCase(uri.getScheme())) {
// 如果是file類型的Uri酪耕,直接獲取圖片路徑即可
? ? ? ? ? ? imagePath = uri.getPath();
? ? ? ? }
displayImage(imagePath); // 根據(jù)圖片路徑顯示圖片
//? ? ? ? mWeiboDialog = WeiboDialogUtils.createLoadingDialog(FeedbackDetails.this, "加載中...");
//? ? ? ? handler.sendEmptyMessageDelayed(1, 2000);
? ? }
private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
? ? ? ? String imagePath = getImagePath(uri, null);
? ? ? ? displayImage(imagePath);
//? ? ? ? mWeiboDialog = WeiboDialogUtils.createLoadingDialog(FeedbackDetails.this, "加載中...");
//? ? ? ? handler.sendEmptyMessageDelayed(1, 2000);
? ? }
private StringgetImagePath(Uri uri, String selection) {
String path =null;
? ? ? ? // 通過Uri和selection來獲取真實的圖片路徑
? ? ? ? Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
? ? ? ? if (cursor !=null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.
Images.Media.DATA));
? ? ? ? ? ? }
cursor.close();
? ? ? ? }
return path;
? ? }
private void displayImage(String imagePath) {
BitmapFactory.Options options =new BitmapFactory.Options();
? ? ? ? options.inJustDecodeBounds =false;
//? ? ? ? Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
? ? ? ? Bitmap bitmap1=getBitmapFromUri(this, getImageContentUri(this,imagePath));
? ? ? ? if (bitmap1 !=null) {
//獲取圖片
//? ? ? ? ? ? File headFile = null;
//把獲取到的圖片放進集合里 每次獲取一個
? ? ? ? ? ? selectList.add(new Image(bitmap1));
? ? ? ? ? ? adapter.setList(selectList);
? ? ? ? ? ? adapter.notifyDataSetChanged();
? ? ? ? ? ? //把bitmap轉(zhuǎn)為數(shù)組
//? ? ? ? ? ? File headFile = saveMyBitmap(bitmap1, "head");
//? ? ? ? ? ? upphoto(headFile);
? ? ? ? }else {
Toast.makeText(this, "獲取圖片失敗", Toast.LENGTH_SHORT).show();
? ? ? ? }
}
// 通過uri加載圖片
? ? public static BitmapgetBitmapFromUri(Context context, Uri uri) {
try {
ParcelFileDescriptor parcelFileDescriptor =
context.getContentResolver().openFileDescriptor(uri, "r");
? ? ? ? ? ? FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
? ? ? ? ? ? Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
? ? ? ? ? ? parcelFileDescriptor.close();
? ? ? ? ? ? return image;
? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? }
return null;
? ? }
public static UrigetImageContentUri(Context context, String path) {
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
? ? ? ? ? ? ? ? new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA +"=? ",
? ? ? ? ? ? ? ? new String[] { path }, null);
? ? ? ? if (cursor !=null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
? ? ? ? ? ? Uri baseUri = Uri.parse("content://media/external/images/media");
? ? ? ? ? ? return Uri.withAppendedPath(baseUri, "" + id);
? ? ? ? }else {
// 如果圖片不在手機的共享圖片數(shù)據(jù)庫,就先把它插入轨淌。
? ? ? ? ? ? if (new File(path).exists()) {
ContentValues values =new ContentValues();
? ? ? ? ? ? ? ? values.put(MediaStore.Images.Media.DATA, path);
? ? ? ? ? ? ? ? return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
? ? ? ? ? ? }else {
return null;
? ? ? ? ? ? }
}
}
//將bitmap轉(zhuǎn)化為png格式
//首先獲取了系統(tǒng)相冊的地址迂烁,然后每次我拍下照片或者選擇照片時,
// 都進行簡單的壓縮递鹉,因為圖片要上傳到服務(wù)器進行審核管理盟步,做成微信頭像之類的超級壓縮方法有損畫質(zhì),
// 所以我只是進行了簡單的壓縮上傳最后寫成一個文件躏结。(在第二個頁面的時候可能會有幾個問題却盘,
// 三個ImageView即是三個bitmap,三個File文件媳拴。如何判斷及正確的顯示黄橘,我們可以定義成全局變量,
// 因為都是點擊事件禀挫,所以bitmap會在點擊后修改旬陡,然后保存下來。
? ? public FilesaveMyBitmap(Bitmap mBitmap, String prefix) {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
? ? ? ? File file =null;
? ? ? ? try {
file = File.createTempFile(
prefix,? /* prefix */
? ? ? ? ? ? ? ? ? ? ".jpg"
//? ? ? ? ? ? ? ? ? ? ,? ? ? ? /* suffix */
//? ? ? ? ? ? ? ? ? ? storageDir? ? ? /* directory */
? ? ? ? ? ? );
? ? ? ? ? ? FileOutputStream fos =new FileOutputStream(file); //創(chuàng)建一個字節(jié)數(shù)組輸出流對象
//Bitmap.CompressFormat format 圖像的壓縮格式语婴;
//int quality 圖像壓縮率描孟,0-100驶睦。 0 壓縮100%,100意味著不壓縮匿醒;
//OutputStream stream 寫入壓縮數(shù)據(jù)的輸出流场航;
? ? ? ? ? ? mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);//通過bitmap中的compress,將圖片壓縮到os流對象中.
//其中第二個參數(shù)quality,為100表示不壓縮,如果為80,表示壓縮百分之20.
//? ? ? ? ? ? Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth()/2, mBitmap.getHeight()/2, true);
? ? ? ? ? ? fos.flush();
? ? ? ? ? ? fos.close();
? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? }
return file;
? ? }
}