Android 仿照微信發說說,既能實現拍照,選相簿,多圖案上傳 使用Retrofit2.0技術
阿新 • • 發佈:2018-12-23
package com.lidong.photopickersample; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.lidong.photopicker.ImageCaptureManager; import com.lidong.photopicker.PhotoPickerActivity; import com.lidong.photopicker.PhotoPreviewActivity; import com.lidong.photopicker.SelectModel; import com.lidong.photopicker.intent.PhotoPickerIntent; import com.lidong.photopicker.intent.PhotoPreviewIntent; import org.json.JSONArray; import java.util.ArrayList; /** * @ * @author lidong * @date 2016-02-29 */ public class MainActivity extends AppCompatActivity { private static final int REQUEST_CAMERA_CODE = 10; private static final int REQUEST_PREVIEW_CODE = 20; private ArrayList<String> imagePaths = new ArrayList<>(); private ImageCaptureManager captureManager; // 相機拍照處理類 private GridView gridView; private GridAdapter gridAdapter; private Button mButton; private String depp; private EditText textView; private String TAG =MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); mButton = (Button) findViewById(R.id.button); textView= (EditText)findViewById(R.id.et_context); int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi; cols = cols < 3 ? 3 : cols; gridView.setNumColumns(cols); // preview gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String imgs = (String) parent.getItemAtPosition(position); if ("000000".equals(imgs) ){ PhotoPickerIntent intent = new PhotoPickerIntent(MainActivity.this); intent.setSelectModel(SelectModel.MULTI); intent.setShowCarema(true); // 是否顯示拍照 intent.setMaxTotal(6); // 最多選擇照片數量,預設為6 intent.setSelectedPaths(imagePaths); // 已選中的照片地址, 用於回顯選中狀態 startActivityForResult(intent, REQUEST_CAMERA_CODE); }else{ PhotoPreviewIntent intent = new PhotoPreviewIntent(MainActivity.this); intent.setCurrentItem(position); intent.setPhotoPaths(imagePaths); startActivityForResult(intent, REQUEST_PREVIEW_CODE); } } }); imagePaths.add("000000"); gridAdapter = new GridAdapter(imagePaths); gridView.setAdapter(gridAdapter); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { depp =textView.getText().toString().trim()!=null?textView.getText().toString().trim():"woowoeo"; new Thread(){ @Override public void run() { super.run(); FileUploadManager.uploadMany(imagePaths, depp); // FileUploadManager.upload(imagePaths,depp); } }.start(); } }); } @Override protected void onResume() { super.onResume(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK) { switch (requestCode) { // 選擇照片 case REQUEST_CAMERA_CODE: ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT); Log.d(TAG, "list: " + "list = [" + list.size()); loadAdpater(list); break; // 預覽 case REQUEST_PREVIEW_CODE: ArrayList<String> ListExtra = data.getStringArrayListExtra(PhotoPreviewActivity.EXTRA_RESULT); Log.d(TAG, "ListExtra: " + "ListExtra = [" + ListExtra.size()); loadAdpater(ListExtra); break; } } } private void loadAdpater(ArrayList<String> paths){ if (imagePaths!=null&& imagePaths.size()>0){ imagePaths.clear(); } if (paths.contains("000000")){ paths.remove("000000"); } paths.add("000000"); imagePaths.addAll(paths); gridAdapter = new GridAdapter(imagePaths); gridView.setAdapter(gridAdapter); try{ JSONArray obj = new JSONArray(imagePaths); Log.e("--", obj.toString()); }catch (Exception e){ e.printStackTrace(); } } private class GridAdapter extends BaseAdapter{ private ArrayList<String> listUrls; private LayoutInflater inflater; public GridAdapter(ArrayList<String> listUrls) { this.listUrls = listUrls; if(listUrls.size() == 7){ listUrls.remove(listUrls.size()-1); } inflater = LayoutInflater.from(MainActivity.this); } public int getCount(){ return listUrls.size(); } @Override public String getItem(int position) { return listUrls.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.item_image, parent,false); holder.image = (ImageView) convertView.findViewById(R.id.imageView); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } final String path=listUrls.get(position); if (path.equals("000000")){ holder.image.setImageResource(R.mipmap.ic_launcher); }else { Glide.with(MainActivity.this) .load(path) .placeholder(R.mipmap.default_error) .error(R.mipmap.default_error) .centerCrop() .crossFade() .into(holder.image); } return convertView; } class ViewHolder { ImageView image; } } }