发布效果图:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReleaseActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="0dp">
<ImageButton
android:id="@+id/back_btn"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentLeft="true"
android:background="@drawable/tab_menu_bg"
android:src="?attr/homeAsUpIndicator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="闲置物品发布"
android:textSize="19sp" />
</RelativeLayout>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:text="标题"/>
<EditText
android:id="@+id/f_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:hint="如八成新自行车、二手书等"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:text="价格"/>
<EditText
android:id="@+id/f_price"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minWidth="100dp"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:text="联系方式"/>
<EditText
android:id="@+id/f_phone"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minWidth="210dp"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="描述"
android:textSize="20dp" />
<EditText
android:id="@+id/f_describe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top"
android:hint="添加详细的描述(细节、购买信息、闲置原因)有助于快速卖出!"
android:lines="5"
android:textSize="20dp" />
<ImageButton
android:id="@+id/m1_image"
android:layout_width="130dp"
android:layout_height="130dp"
android:src="@mipmap/m1_sc" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center"
android:orientation="vertical">
<Button
android:id="@+id/f_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
android:background="@color/blue"
android:text="发布"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
ReleaseActivity.java
package com.example.instructorhelpdome;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import com.example.instructorhelpdome.JavaBean.MyUser;
import com.example.instructorhelpdome.JavaBean.Release;
import com.fingerth.supdialogutils.SYSDiaLogUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;
public class ReleaseActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton back_btn;
private EditText f_title;
private EditText f_price;
private EditText f_phone;
private EditText f_describe;
private byte[] image;
private Button fbtn;
private ImageButton imageButton;
public Release mRelease=new Release();
String imagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_release);
initView();
}
private void initView() {
back_btn=(ImageButton)findViewById(R.id.back_btn);
back_btn.setOnClickListener(this);
f_title=(EditText)findViewById(R.id.f_title);
f_price=(EditText)findViewById(R.id.f_price);
f_phone=(EditText)findViewById(R.id.f_phone);
f_describe=(EditText)findViewById(R.id.f_describe);
fbtn=(Button)findViewById(R.id.f_btn);
fbtn.setOnClickListener(this);
imageButton=(ImageButton)findViewById(R.id.m1_image);
imageButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.back_btn:
finish();
break;
case R.id.f_btn:
ReleaseBtn();
break;
case R.id.m1_image:
toImages();
break;
}
}
private void toImages() {
if (ContextCompat.checkSelfPermission(ReleaseActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ReleaseActivity.this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
//打开系统相册
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片路径
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
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);
Log.i("msg",imagePath);
showImage(imagePath);
c.close();
}
}
//加载图片
private void showImage(String imaePath) {
Bitmap bm = BitmapFactory.decodeFile(imaePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
image = baos.toByteArray();
imageButton.setImageBitmap(bm);
}
private void ReleaseBtn() {
Log.i("msg",imagePath);
final BmobFile imageFile = new BmobFile(new File(imagePath));
imageFile.uploadblock(new UploadFileListener() {
@Override
public void done(BmobException e) {
if (e==null){
Log.i("msg","上传成功");
saveR(imageFile);
}else {
Log.i("msg","上传失败"+e.getMessage());
}
}
});
}
private void saveR(BmobFile imageFile) {
MyUser userInfo = BmobUser.getCurrentUser(MyUser.class);
String f_name=userInfo.getUsername();
mRelease.setUsername(f_name);
String s_title=f_title.getText().toString();
mRelease.setTitle(s_title);
String s_price=f_price.getText().toString();
mRelease.setPrice(s_price);
String s_phone=f_phone.getText().toString();
mRelease.setPhone(s_phone);
String s_describe=f_describe.getText().toString();
mRelease.setDescribe(s_describe);
mRelease.setImages(imageFile);
mRelease.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if (e==null){
Log.i("发布成功",s);
SYSDiaLogUtils.showSuccessDialog(ReleaseActivity.this, "二手消息发布成功", "恭喜你,操作成功了!", "OK", false);
}else {
Log.i("失败",e.getMessage());
SYSDiaLogUtils.showErrorDialog(ReleaseActivity.this, "失败警告", "很抱歉,这次更新失败,請检查网络重新試試!", "取消", false);
}
}
});
}
}
数据库数据