android多圖片選擇

忽然覺得簡書不適合分享代碼 看著很亂 所以我上博客分享了我的代碼

首先記錄一下寫的比較好的二個

這個是調(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;

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市廉羔,隨后出現(xiàn)的幾起案子溉痢,更是在濱河造成了極大的恐慌,老刑警劉巖憋他,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件孩饼,死亡現(xiàn)場離奇詭異,居然都是意外死亡竹挡,警方通過查閱死者的電腦和手機镀娶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來揪罕,“玉大人梯码,你說我怎么就攤上這事『脝” “怎么了轩娶?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長框往。 經(jīng)常有香客問我鳄抒,道長,這世上最難降的妖魔是什么搅窿? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任嘁酿,我火速辦了婚禮,結(jié)果婚禮上男应,老公的妹妹穿的比我還像新娘闹司。我一直安慰自己,他們只是感情好沐飘,可當我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布游桩。 她就那樣靜靜地躺著,像睡著了一般耐朴。 火紅的嫁衣襯著肌膚如雪借卧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天筛峭,我揣著相機與錄音铐刘,去河邊找鬼。 笑死影晓,一個胖子當著我的面吹牛镰吵,可吹牛的內(nèi)容都是我干的檩禾。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼疤祭,長吁一口氣:“原來是場噩夢啊……” “哼盼产!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起勺馆,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤戏售,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后草穆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體灌灾,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年悲柱,在試婚紗的時候發(fā)現(xiàn)自己被綠了紧卒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡诗祸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出轴总,到底是詐尸還是另有隱情直颅,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布怀樟,位于F島的核電站功偿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏往堡。R本人自食惡果不足惜械荷,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望虑灰。 院中可真熱鬧吨瞎,春花似錦、人聲如沸穆咐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽对湃。三九已至崖叫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拍柒,已是汗流浹背心傀。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拆讯,地道東北人脂男。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓养叛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親疆液。 傳聞我的和親對象是個殘疾皇子一铅,可洞房花燭夜當晚...
    茶點故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容