一、明日计划
继续完成PDF文件的上传与展示
二、今天遇到的问题
在开发过程中,先是完成了数据库层的基本操作语句,之后修改表结构,添加文件的二进制转化,最后卡在了byte[]数组转化为Blob类型上,尝试了许多解决办法如hibernate,并不是很有效
这是目前的项目结构
这是当下的表结构,明天继续加油
部分原码
package com.example.fooks; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.nfc.Tag; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import androidx.annotation.Nullable; import com.example.fooks.dao.dao; import com.example.fooks.entity.Book; import com.example.fooks.utils.BooksAdapter; import com.example.fooks.utils.ViewHolder; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ReadActivity extends Activity { private Button mHome; private Button mBook; private Button mPerson; private Button mUpload; private ListView mListView; private List<Book> mShowBooks =new ArrayList<>(); private List <ViewHolder> mViewHolder; private static final int FILE_SELECT_CODE = 0; private static String Tag="ReadActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read); mListView=(ListView) this.findViewById(R.id.book_list) ; //初始化控件 initView(); initListener(); ListView ShowBookList=(ListView)findViewById(R.id.book_list); //获取数据 dao dao=new dao(); mShowBooks=dao.Query(); BooksAdapter booksAdapter=new BooksAdapter(mShowBooks,this); ShowBookList.setAdapter(booksAdapter); } private void initListener() { mUpload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { upLoad(); } }); } private void upLoad() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*");//过滤文件类型(所有) intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(Intent.createChooser(intent, "请选择文件!"), FILE_SELECT_CODE); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(this, "未安装文件管理器!", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case FILE_SELECT_CODE: if (resultCode == RESULT_OK) { Uri uri = data.getData(); String path = FileUtils.getPath(this, uri);//得到文件路径 Log.e(Tag,path); File file=new File(path); int dot = file.getName().lastIndexOf('.'); String name =file.getName().substring(0,dot); Book book=new Book(); book.setBookName(name); book.setBookPath(path); ResultSet rs = null; Blob blob=null ; try { blob.setBytes(1,getBytesFromFile(file)); } catch (SQLException e) { e.printStackTrace(); } book.setUpBook(blob); book.setCreateUser("张志伟"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); Date date = new Date(); String createdate = simpleDateFormat.format(date); book.setCreateDate(createdate); } break; } } /** * 把文件转换为byte文件以保存到数据库 */ public static byte[] getBytesFromFile(File f){ if (f == null){ return null; } try { FileInputStream stream = new FileInputStream(f); ByteArrayOutputStream out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = stream.read(b)) != -1) out.write(b, 0, n); stream.close(); out.close(); return out.toByteArray(); } catch (IOException e){ } return null; } static class FileUtils { public static String getPath(Context context, Uri uri) { if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { return cursor.getString(column_index); } } catch (Exception e) { e.printStackTrace(); } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } } private void initView() { mBook=(Button) this.findViewById(R.id.btn_book); mHome=(Button) this.findViewById(R.id.btn_home); mPerson=(Button) this.findViewById(R.id.btn_person); mUpload=(Button) this.findViewById(R.id.book_upload); } }