从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

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、