从android选取文件获取文件路径,并将文件读入到数据库中,有进度条显示

2024-06-20 03:32

本文主要是介绍从android选取文件获取文件路径,并将文件读入到数据库中,有进度条显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

大概流程:

点击选择文件按钮,出来MyFileManager对话框,从而选择文件路径,获取文件路径,对文件进行读取,并插入到数据库中

选取文件获取文件名及路径方法   PS:被屏蔽掉的代码,如果恢复正常,则可以判断文件类型,从而进行打开

import java.io.File;
import java.util.ArrayList;
import java.util.List;import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;public class MyFileManager extends ListActivity {private List<String> items = null;private List<String> paths = null;private String rootPath = "/";private String curPath = "/";private TextView mPath;private final static String TAG = "bb";@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.fileselect);mPath = (TextView) findViewById(R.id.mPath);
//		Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
//		buttonConfirm.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				Intent data = new Intent(MyFileManager.this, MainActivity.class);
//				Bundle bundle = new Bundle();
//				bundle.putString("file", curPath);
//				data.putExtras(bundle);
//				setResult(2, data);
//				finish();
//
//			}
//		});
//		Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
//		buttonCancle.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				finish();
//			}
//		});getFileDir(rootPath);}private void getFileDir(String filePath) {mPath.setText(filePath);items = new ArrayList<String>();paths = new ArrayList<String>();File f = new File(filePath);File[] files = f.listFiles();if (!filePath.equals(rootPath)) {items.add("b1");paths.add(rootPath);items.add("b2");paths.add(f.getParent());}for (int i = 0; i < files.length; i++) {File file = files[i];items.add(file.getName());paths.add(file.getPath());}setListAdapter(new MyAdapter(this, items, paths));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {File file = new File(paths.get(position));if (file.isDirectory()) {curPath = paths.get(position);getFileDir(paths.get(position));}else{//	String fName = file.getName();curPath=paths.get(position);Intent data = new Intent(MyFileManager.this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("file", curPath);data.putExtras(bundle);setResult(2, data);finish();}
//		else {
//			openFile(file);
//		}}//	private void openFile(File f) {
//		Intent intent = new Intent();
//		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		intent.setAction(android.content.Intent.ACTION_VIEW);
//
//		String type = getMIMEType(f);
//		intent.setDataAndType(Uri.fromFile(f), type);
//		startActivity(intent);
//	}//	private String getMIMEType(File f) {
//		String type = "";
//		String fName = f.getName();
//		String end = fName
//				.substring(fName.lastIndexOf(".") + 1, fName.length())
//				.toLowerCase();
//
//		if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
//				|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
//			type = "audio";
//		} else if (end.equals("3gp") || end.equals("mp4")) {
//			type = "video";
//		} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
//				|| end.equals("jpeg") || end.equals("bmp")) {
//			type = "image";
//		} else {
//			type = "*";
//		}
//		type += "/*";
//		return type;
//	}
}





读取文件并向手机数据库中插入文件记录适配器


public class MyDBAdapter {private static String fileName;private static final String DATABASE_NAME="sense.db";private static final String DATABASE_TABLE="sense";private static final int DATABASE_VERSION=1;private static final String DATABASE_CREATE="create table "+DATABASE_TABLE+"(_id integer primary key autoincrement, lac char(8),cid char(8),name varchar(40))";private SQLiteDatabase db;private final Context context;private myDbHelper dbHelper;public MyDBAdapter(Context context,String fileName){this.context=context;this.fileName=fileName;dbHelper=new myDbHelper(context,DATABASE_NAME,null,DATABASE_VERSION);}public MyDBAdapter(Context context){this.context=context;dbHelper=new myDbHelper(context,DATABASE_NAME,null,DATABASE_VERSION);}//	public boolean existDatabase(){
//		boolean flag=false;
//		try{
//			
//			flag=true;
//		}catch(FileNotFoundException e){
//			flag=false;
//		}
//		return flag;
//	}public boolean getcount(){boolean flag=false;Cursor cursor = null;try {cursor= db.query(DATABASE_TABLE,null,null,null,null,null,null);System.out.println("总数据"+cursor.getCount());

这篇关于从android选取文件获取文件路径,并将文件读入到数据库中,有进度条显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1076979

相关文章

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们

使用Python实现获取网页指定内容

《使用Python实现获取网页指定内容》在当今互联网时代,网页数据抓取是一项非常重要的技能,本文将带你从零开始学习如何使用Python获取网页中的指定内容,希望对大家有所帮助... 目录引言1. 网页抓取的基本概念2. python中的网页抓取库3. 安装必要的库4. 发送HTTP请求并获取网页内容5. 解