package com.bawei.zouye20190918.view.activity;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.bawei.zouye20190918.R;
import com.bawei.zouye20190918.contesact.ZConesact;
import com.bawei.zouye20190918.model.bean.QuanBean;
import com.bawei.zouye20190918.model.http.HttpApi;
import com.bawei.zouye20190918.model.http.HttpUtils;
import com.bawei.zouye20190918.model.http.ImageUtil;
import com.bawei.zouye20190918.presenter.CirclePresenter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity implements ZConesact.Zview {
private Button btn;
private List<MultipartBody.Part> partList = new ArrayList<>();
private List<String> imagePaths = new ArrayList<>();
private LinearLayout linear_image;
private EditText edit;
private Button pinglun;
@Override
public void success(QuanBean bean) {
}
@Override
protected void initData() {
pinglun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,HomeActivity.class));
Toast.makeText(MainActivity.this, "点击跳转", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
//获取评论内容
String editsing = edit.getText().toString().trim();
//点击按钮访问图库
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
Retrofit retrofit = HttpUtils.getHttpUils().getRetrofit();
HttpApi httpApi = retrofit.create(HttpApi.class);
Observable<ResponseBody> circle = httpApi.circle(7452, "15690505883507452", 4, editsing, partList);
circle.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String string = responseBody.string();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
//上传图片
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
String path = ImageUtil.getImageAbsolutePath(MainActivity.this, uri);
//将图片存入集合
imagePaths.add(path);
//清空集合
partList.clear();
if (imagePaths.size()>0 && imagePaths.size()<5){
for (int i = 0; i < imagePaths.size(); i++) {
ImageView imageView = (ImageView) linear_image.getChildAt(i);
Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(i));
imageView.setImageBitmap(bitmap);
File file = new File(imagePaths.get(i));
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
partList.add(part);
}
}else{
Toast.makeText(this, "最多上传四张图片", Toast.LENGTH_SHORT).show();
}
}
@Override
protected CirclePresenter setPresenter() {
return new CirclePresenter();
}
@Override
protected void initView() {
edit = findViewById(R.id.main_edit);
btn = findViewById(R.id.main_btn);
linear_image = findViewById(R.id.linear_image);
pinglun = findViewById(R.id.main_pinglun);
}
@Override
protected int setLayout() {
return R.layout.activity_main;
}
}
XML
<EditText
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="请输入评论"
android:id="@+id/main_edit"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片上传"
android:id="@+id/main_btn"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/linear_image"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_margin="5dp"></ImageView>
<ImageView
android:id="@+id/image2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_margin="5dp"></ImageView>
<ImageView
android:id="@+id/image3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_margin="5dp"></ImageView>
<ImageView
android:id="@+id/image4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_margin="5dp"></ImageView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_pinglun"
android:text="提交评论"></Button>
方式二
package com.example.rxrretrofit.activity;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.rxrretrofit.R;
import com.example.rxrretrofit.bean.Result;
import com.example.rxrretrofit.bean.User;
import com.example.rxrretrofit.db.UserDao;
import com.example.rxrretrofit.http.DataCall;
import com.example.rxrretrofit.presenter.SendImagePresenter;
import butterknife.BindView;
import butterknife.OnClick;
public class SendImageActivity extends BaseActivity{
@BindView(R.id.image)
ImageView mImage;
@BindView(R.id.text)
EditText mText;
@BindView(R.id.btn_send)
Button mSend;
SendImagePresenter sendImagePresenter;
User data;
@Override
protected void initView(Bundle savedInstanceState) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
sendImagePresenter = new SendImagePresenter(new ImageCall());
data = userDao.queryBuilder().where(UserDao.Properties.Login.eq(true)).unique();
}
@Override
protected int getLayoutId() {
return R.layout.activity_add_circle;
}
@OnClick(R.id.image)
public void image(View view) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 100);
}
String filePath;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 100) {//resultcode是setResult里面设置的code值
Uri uri = data.getData();
Glide.with(this).load(uri).into(mImage);
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
filePath = actualimagecursor
.getString(actual_image_column_index);
// 4.0以上平台会自动关闭cursor,所以加上版本判断,OK
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
actualimagecursor.close();
}
}
@OnClick(R.id.btn_send)
public void send(View view) {
sendImagePresenter.requestData(String.valueOf(data.getUserId()),data.getSessionId(),
mText.getText().toString(),filePath);
}
class ImageCall implements DataCall{
@Override
public void success(Object data) {
Toast.makeText(getBaseContext(),"上传成功",Toast.LENGTH_LONG).show();
}
@Override
public void fail(Result result) {
}
}
}
XML
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="发布"/>
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_below="@+id/btn_send"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:gravity="center"
android:hint="心情如何..."
android:maxLength="140"
android:padding="15dp"
android:textSize="16sp"></EditText>
<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/text"
android:src="@mipmap/ic_launcher"
android:layout_alignParentLeft="true">
</ImageView>