今天做了什么:

完成了发帖界面的编辑,并且存入数据库

package com.hui.testend.community;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NavUtils;
import androidx.core.content.ContextCompat;

import android.Manifest;
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.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hui.testend.MainActivity;
import com.hui.testend.R;
import com.hui.testend.mytool.Dao;
import com.hui.testend.mytool.ImageUtil;
import com.hui.testend.mytool.addtemp;
import com.hui.testend.recongnize.MyObject;
import com.hui.testend.recongnize.altest;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class addcontent extends AppCompatActivity implements View.OnClickListener {
Handler mainhandler;
ImageView iv_add_photo;
Button btn_add_comm,btn_cancel;
Uri imageUri;
EditText ed_title,ed_content;
public int REQUEST_CODE_CHOOSE=0;
Bitmap addbitmap;
Dao dao=new Dao();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontent);
init();
try {
getdata();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public void init(){
iv_add_photo=findViewById(R.id.iv_add_photo);
btn_add_comm=findViewById(R.id.btn_add_comm);
btn_cancel=findViewById(R.id.btn_cancel);
ed_content=findViewById(R.id.ed_content);
ed_title=findViewById(R.id.ed_title);
btn_cancel.setOnClickListener(this);
btn_add_comm.setOnClickListener(this);
iv_add_photo.setOnClickListener(this);
mainhandler=new Handler(getMainLooper());
}

public void getdata() throws FileNotFoundException {
Intent intent=getIntent();
Bundle temp=intent.getBundleExtra("temp");
iv_add_photo.setImageURI(Uri.parse(temp.getString("img")));
ed_title.setText(temp.getString("title"));
ed_content.setText(temp.getString("content"));

if (temp.getInt("a")==1){
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(temp.getString("img")));
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
addbitmap=bitmap;
}
else{
String imagePath = getPathFromUri(Uri.parse(temp.getString("img")));
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
addbitmap=bitmap;
}
temp.getString("img");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 0:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String imagePath = getPathFromUri(uri);
new Thread(new Runnable() {
@Override
public void run() {
mainhandler.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
addbitmap=bitmap;
iv_add_photo.setImageBitmap(bitmap);
}
});
}
}).start();
break;
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 0:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限被授予,可以进行相应的操作
// 在这里进行访问文件的操作
dochoose();
} else {
Toast.makeText(this, "需要读取外部存储权限才能访问文件", Toast.LENGTH_SHORT).show();
}
break;
}
}
private void dochoose() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_CHOOSE);
}
public void choosePhoto(View view){
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// 真正的执行去相册
dochoose();
} else {
// 去申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
}
private String getPathFromUri(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
public void addcomm(String title, String content, String bitmap){
new Thread(new Runnable() {
@Override
public void run() {
dao.add_comm(title,content,bitmap);
mainhandler.post(new Runnable() {
@Override
public void run() {

}
});
}
}).start();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.iv_add_photo:
choosePhoto(v);
break;
case R.id.btn_add_comm:
System.out.println(999999);
String title=ed_title.getText().toString().trim();
System.out.println(title);
String content=ed_content.getText().toString().trim();
// System.out.println(ImageUtil.imageToBase64(addbitmap));
addcomm(title,content,ImageUtil.imageToBase64(addbitmap));
tiao();
break;
case R.id.btn_cancel:
finish();
//NavUtils.navigateUpFromSameTask(this);
break;
}
}
public void tiao(){
Intent intent=new Intent(this, MainActivity.class);
startActivity(intent);
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".community.addcontent"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:background="@color/white">
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 动态"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1">

<Button
android:id="@+id/btn_add_comm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/btn_bg_selector"
android:text="发表" />

</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@color/gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题:"
android:textSize="20sp"/>
<EditText
android:id="@+id/ed_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正文:"
android:textSize="20sp"/>
<EditText
android:id="@+id/ed_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:minLines="6"
/>
</LinearLayout>
<ImageView
android:id="@+id/iv_add_photo"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/img"
/>

</LinearLayout>

遇到了什么困难:

明天准备做什么:

继续写作业

代码量 100行

时间 0.8小时.