Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用Retrofit2.0技術仿微信發說說

Android使用Retrofit2.0技術仿微信發說說

編輯:關於Android編程

最近項目做完了,有閒暇時間,一直想做一個類似微信中微信發說說,既能實現拍照,選圖庫,多圖案上傳的案例,目前好多App都有類似微信朋友圈的功能,能過發表說說等附帶圖片上傳。下面的就是實現該功能的過程:大家還沒有看過Android Retrofit 2.0框架上傳圖片解決方案(一張與多張的處理)這篇文章,在看今天的就很容易,接在本項目中用到了一個library:photopicker,封裝了圖片的選擇功能,是否選相機,還有選中圖片後可以查看圖片的功能。 

一、 首先:將photopicker到工程中

(1)、先簡單講解一下PhotoPickerIntent的用法:         

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


(2)、設置好之後,重寫onActivityResult方法處理選中圖片和預覽加載適配器

@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: 
   loadAdpater(data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT)); 
   break; 
  // 預覽 
  case REQUEST_PREVIEW_CODE: 
   loadAdpater(data.getStringArrayListExtra(PhotoPreviewActivity.EXTRA_RESULT)); 
   break; 
  } 
 } 
 } 

 二、重點在GridAdapter

1.在圖片路徑中默認添加一圖片,用來調用需選擇圖庫
imagePaths.add("000000"); 

2.根據路徑判斷選中的圖片。如果超過6張,默認路徑從集合中移除。

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

三、上傳管理類

/** 
 * Created by lidong on 2016/1/28. 
 */ 
public class FileUploadManager { 
 
 private static final String ENDPOINT = "http://192.168.1.21:8080"; 
 private static String TAG = FileUploadManager.class.getSimpleName(); 
 
 public interface FileUploadService { 
 /** 
  * 上傳一張圖片 
  * @param description 
  * @param imgs 
  * @return 
  */ 
 @Multipart 
 @POST("/upload") 
 Call<String> uploadImage(@Part("fileName") String description, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs); 
 
 
 /** 
  * 上傳6張圖片 
  * @param description 
  * @param imgs1 
  * @param imgs2 
  * @param imgs3 
  * @param imgs4 
  * @param imgs5 
  * @param imgs6 
  * @return 
  */ 
 @Multipart 
 @POST("/upload") 
 Call<String> uploadImage(@Part("description") String description, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs1, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs2, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs3, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs4, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs5, 
     @Part("file\"; filename=\"image.png\"") RequestBody imgs6); 
 
 /** 
  * 簡便寫法 
  * @param description 
  * @param imgs1 
  * @return 
  */ 
 @Multipart 
 @POST("/upload") 
 Call<String> uploadImage(@Part("description") String description,@PartMap 
     Map<String, RequestBody> imgs1); 
 } 
 
 private static final Retrofit sRetrofit = new Retrofit .Builder() 
  .baseUrl(ENDPOINT) 
  .addConverterFactory(GsonConverterFactory.create()) 
  .build(); 
 
 private static final FileUploadService apiManager = sRetrofit.create(FileUploadService.class); 
 
 
 /** 
 * 發說說 
 * @param paths 
 * @param desp 
 */ 
 public static void upload(ArrayList<String> paths,String desp){ 
 RequestBody[] requestBody= new RequestBody[6]; 
 if (paths.size()>0) { 
  for (int i=0;i<paths.size();i++) { 
  requestBody[i] = 
   RequestBody.create(MediaType.parse("multipart/form-data"), new File(paths.get(i))); 
  } 
 } 
 Call<String> call = apiManager.uploadImage( desp,requestBody[0],requestBody[1],requestBody[2],requestBody[3],requestBody[4],requestBody 
 
[5]); 
 call.enqueue(new Callback<String>() { 
  @Override 
  public void onResponse(Call<String> call, Response<String> response) { 
  Log.d(TAG, "onResponse() called with: " + "call = [" + call + "], response = [" + response + "]"); 
  } 
 
  @Override 
  public void onFailure(Call<String> call, Throwable t) { 
  Log.d(TAG, "onFailure() called with: " + "call = [" + call + "], t = [" + t + "]"); 
  } 
 }); 
 
 }<pre name="code" class="java"> /** 
 * 
 * @param paths 
 * @param desp 
 */ 
 public static void uploadMany(ArrayList<String> paths,String desp){ 
 Map<String,RequestBody> photos = new HashMap<>(); 
 if (paths.size()>0) { 
  for (int i=0;i<paths.size();i++) { 
  String substring = paths.get(i).substring(paths.get(i).lastIndexOf("/") + 1, paths.get(i).length()); 
  photos.put("file\"; filename="+substring, RequestBody.create(MediaType.parse("multipart/form-data"), new File(paths.get(i)))); 
  } 
 } 
 Call<String> stringCall = apiManager.uploadImage(desp, photos); 
 stringCall.enqueue(new Callback<String>() { 
  @Override 
  public void onResponse(Call<String> call, Response<String> response) { 
  Log.d(TAG, "onResponse() called with: " + "call = [" + call + "], response = [" + response + "]"); 
  } 
 
  @Override 
  public void onFailure(Call<String> call, Throwable t) { 
  Log.d(TAG, "onFailure() called with: " + "call = [" + call + "], t = [" + t + "]"); 
  } 
 }); 
 } 

四、項目代碼

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

五、SpringMVC接收文件的action

 public String addUser(@RequestParam("file") CommonsMultipartFile[] files, 
  HttpServletRequest request){ 
  
 for(int i = 0;i<files.length;i++){ 
  System.out.println("fileName---------->" + files[i].getOriginalFilename()); 
  
  if(!files[i].isEmpty()){ 
  int pre = (int) System.currentTimeMillis(); 
  try { 
   //拿到輸出流,同時重命名上傳的文件 
   FileOutputStream os = new FileOutputStream("f:/img"+"/" + new Date().getTime()+".jpg"); 
   //拿到上傳文件的輸入流 
   FileInputStream in = (FileInputStream) files[i].getInputStream(); 
   
   //以寫字節的方式寫文件 
   int b = 0; 
   while((b=in.read()) != -1){ 
   os.write(b); 
   } 
   os.flush(); 
   os.close(); 
   in.close(); 
   int finaltime = (int) System.currentTimeMillis(); 
   System.out.println(finaltime - pre); 
   
  } catch (Exception e) { 
   e.printStackTrace(); 
   System.out.println("上傳出錯"); 
  } 
 } 
 } 
 return "/success"; 
 } 

 六、Struts2接收文件

public class UploadFile extends ActionSupport { 
 
 /** 
 * 
 */ 
private static final long serialVersionUID = 1L; 
 
private File[] file;//文件數組 
private String description;//說說內容 
public File[] getFile() { 
 return file; 
} 
public void setFile(File[] file) { 
 this.file = file; 
} 
 
 
public String getDescription() { 
 return description; 
} 
public void setDescription(String description) { 
 this.description = description; 
} 
@Action("/upload") 
public void upload() { 
 System.out.println("上傳的文件="+Arrays.toString(file)); 
 System.out.println("說說內容="+description); 
} 
} 

項目下載地址:https://github.com/lidong1665/Android-UploadMultipartImage

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved