1. 程式人生 > >Android 多張圖片同時上傳到伺服器

Android 多張圖片同時上傳到伺服器

有兩種方式

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String url = "http://172.16.52.17:8080/UploadDemo4/UploadFile";
    private Button get_photo;
    private GridView gv;
    private List<Bitmap> imageUrl;
    private Bitmap bm;
    private MyAdapter myAdapter;
    private Button up_load;
    private Intent intent;
    private String imagePath;
    private List<String> list;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initAdapter();
    }

    private void initAdapter() {
        myAdapter = new MyAdapter(imageUrl, bm, this);
        gv.setAdapter(myAdapter);
    }

    private void initData() {
        imageUrl = new ArrayList<>();
        list = new ArrayList<>();
    }

    private void initView() {
        get_photo = (Button) findViewById(R.id.get_photo);
        get_photo.setOnClickListener(this);
        gv = (GridView) findViewById(R.id.gv);
        up_load = (Button) findViewById(R.id.up_load);
        up_load.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_photo:
//開啟相簿

                intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 1);
                break;
            case R.id.up_load:
                upLoad(list);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                Uri selectedImage = data.getData();
                String[] filePathColumns = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePathColumns[0]);
                imagePath = c.getString(columnIndex);
                showImage(imagePath);
                c.close();
                break;

        }
    }

    private void upLoad(List<String> list) {
        if (list != null) {
            OkHttpClient okHttpClient = new OkHttpClient();
            for (int i = 0; i < list.size(); i++) {

                file = new File(list.get(i));

                MultipartBody.Builder builder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file));
                RequestBody requestBody = builder.build();
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("TAG", "onFailure: " + e);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("TAG", "成功" + response);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        }


    }

    private void showImage(String imagePath) {
        list.add(imagePath);
        bm = BitmapFactory.decodeFile(imagePath);
        Log.e("TAG", "showImage:---- " + imagePath);
        imageUrl.add(bm);
        myAdapter.notifyDataSetChanged();
    }

2.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String url = "http://172.16.52.17:8080/UploadDemo4/UploadFile";

    private Button get_photo;
    private Button up_load;
    private GridView gv;
    private List<String> pathList;
    private List<Bitmap> bitmapList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initAdapter() {
        MyAdapter myAdapter = new MyAdapter(pathList, this);
        gv.setAdapter(myAdapter);
    }



    private void initView() {
        get_photo = (Button) findViewById(R.id.get_photo);
        up_load = (Button) findViewById(R.id.up_load);
        gv = (GridView) findViewById(R.id.gv);

        get_photo.setOnClickListener(this);
        up_load.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_photo:
                MultiImageSelector.create(this)
                        .showCamera(false) // 是否顯示相機. 預設為顯示
                        .count(9) // 最大選擇圖片數量, 預設為9. 只有在選擇模式為多選時有效
                        //.single() // 單選模式
                        .multi() // 多選模式, 預設模式;
                        .start(this, 1);
                break;
            case R.id.up_load:
                upload(pathList);
                break;
        }
    }

    private void upload(List<String> list) {
        if (list != null) {
            OkHttpClient okHttpClient = new OkHttpClient();
            for (int i = 0; i < list.size(); i++) {

              File file = new File(list.get(i));

                MultipartBody.Builder builder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file));
                RequestBody requestBody = builder.build();
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("TAG", "onFailure: " + e);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("TAG", "成功" + response);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            // 獲取返回的圖片列表(存放的是圖片路徑)
            pathList = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            // 處理你自己的邏輯 ....
            initAdapter();
            Log.d("tag", "" + pathList);

        }
    }