从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

相关文章

MySQL Workbench工具导出导入数据库方式

《MySQLWorkbench工具导出导入数据库方式》:本文主要介绍MySQLWorkbench工具导出导入数据库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录mysql Workbench工具导出导入数据库第一步 www.chinasem.cn数据库导出第二步

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

通过C#获取Excel单元格的数据类型的方法详解

《通过C#获取Excel单元格的数据类型的方法详解》在处理Excel文件时,了解单元格的数据类型有助于我们正确地解析和处理数据,本文将详细介绍如何使用FreeSpire.XLS来获取Excel单元格的... 目录引言环境配置6种常见数据类型C# 读取单元格数据类型引言在处理 Excel 文件时,了解单元格

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

查看MySQL数据库版本的四种方法

《查看MySQL数据库版本的四种方法》查看MySQL数据库的版本信息可以通过多种方法实现,包括使用命令行工具、SQL查询语句和图形化管理工具等,以下是详细的步骤和示例代码,需要的朋友可以参考下... 目录方法一:使用命令行工具1. 使用 mysql 命令示例:方法二:使用 mysqladmin 命令示例:方

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp