android多圖片選擇

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

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

這個是調(diào)用PictureSelector?依賴來實(shí)現(xiàn)的?PictureSelector 主要是幫助多圖選取的(簡單)http://www.itdecent.cn/p/48fd284b70a6

這個完全是自己實(shí)現(xiàn)的(困難)?http://www.itdecent.cn/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


自己實(shí)現(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="您的手機(jī)號、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="請留下您寶貴的意見,字?jǐn)?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 {

//用于判斷是否顯示圖標(biāo)

//有圖片

? ? public static final int TYPE_CAMERA =1;

? ? //沒有圖片

? ? public static final int TYPE_PICTURE =2;

? ? private LayoutInflatermInflater;

? ? private Contextcontext;

? ? private int selectMax =4;

? ? /**

* 點(diǎn)擊添加圖片跳轉(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ù)添加的圖標(biāo)

? ? ? ? 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ù)下標(biāo)越界,具體可參考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 的點(diǎn)擊事件

? ? ? ? ? ? if (mItemClickListener !=null) {

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? ? ? ? ? public void onClick(View v) {

int adapterPosition =viewHolder.getAdapterPosition();

? ? ? ? ? ? ? ? ? ? ? ? mItemClickListener.onItemClick(adapterPosition, v);

? ? ? ? ? ? ? ? ? ? }

});

? ? ? ? ? ? }

}

}

//點(diǎn)擊事件接口

? ? 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();

? ? ? ? }

}

//形式:條件表達(dá)式 ?程序塊1 :程序塊2 ;

//含義:如果條件表達(dá)式為真,則本程序語句執(zhí)行程序塊1,程序塊1的值即為整個表達(dá)式的值, 否則為表達(dá)式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) {

// 判斷手機(jī)系統(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來獲取真實(shí)的圖片路徑

? ? ? ? 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;

//把獲取到的圖片放進(jìn)集合里 每次獲取一個

? ? ? ? ? ? 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 {

// 如果圖片不在手機(jī)的共享圖片數(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)相冊的地址,然后每次我拍下照片或者選擇照片時,

// 都進(jìn)行簡單的壓縮,因?yàn)閳D片要上傳到服務(wù)器進(jìn)行審核管理,做成微信頭像之類的超級壓縮方法有損畫質(zhì),

// 所以我只是進(jìn)行了簡單的壓縮上傳最后寫成一個文件。(在第二個頁面的時候可能會有幾個問題,

// 三個ImageView即是三個bitmap,三個File文件。如何判斷及正確的顯示,我們可以定義成全局變量,

// 因?yàn)槎际屈c(diǎn)擊事件,所以bitmap會在點(diǎn)擊后修改,然后保存下來。

? ? 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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1-------- 走進(jìn)前端 2-------- jQuery 3-------- CSS 4-------- A...
    依依玖玥閱讀 2,411評論 0 34
  • 網(wǎng)站: http://ui4app.com/category 技術(shù)網(wǎng)站: http://www.bestsdk.c...
    LYPC_下里巴人閱讀 1,289評論 0 1
  • 前端知識結(jié)構(gòu)https://github.com/JacksonTian/fks Web前端開發(fā)大系概覽https...
    柴東啊閱讀 1,166評論 0 10
  • 原創(chuàng)鏈接 一、Java面試題java有多重要,對于做android的我們,不需要多說了,let’s go (1)J...
    李福來閱讀 2,436評論 0 5
  • 事件:看到朋友圈一位原始點(diǎn)的同學(xué)治療肩周炎的信息。 感受: 高興 感受的感受:靈活 相信 想法觀點(diǎn):這下同事的病...
    王兆雙閱讀 256評論 0 0

友情鏈接更多精彩內(nèi)容