当前位置:首页 » 安卓系统 » android获取文件名

android获取文件名

发布时间: 2022-07-14 22:49:25

A. android 点击图片如何获取图片的文件名谢谢

长按or双击or按菜单键
应该可以出现“属性”的选项
打开就有

B. android里如何解析音频文件获取标题、专辑、文件名、艺术家

把文件放在res/raw下,程序运行时把它释放到指定目录,代码如下:(供楼主参考)

private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/db_exam";
private final String DATABASE_FILENAME = "tel.db";

public void extractDBFileFromRes(){
try {
String dbFileName = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists()){
dir.mkdir();
Log.i("SQLite", "dir made:" + DATABASE_PATH);
} else {
Log.i("SQLite", "dir exist:" + DATABASE_PATH);
}

try {
//如果数据库已经在SD卡的目录下存在,那么不需要重新创建,否则创建文件,并拷贝/res/raw下面的数据库文件
if (!(new File(dbFileName).exists())){
Log.i("SQLite", dbFileName + ":file not exist");
//res/raw数据库作为输出流
InputStream inputStream = this.getResources().openRawResource(R.raw.tel);
//测试
int size = inputStream.available();
Log.i("SQLite", "DATABASE_SIZE:" + 1);
Log.i("SQLite", "count:" + 0);
//用于存放数据库信息的数据流
FileOutputStream fileOutputStream = new FileOutputStream(dbFileName);
byte[] buffer = new byte[8192];
int count = 0;
Log.i("SQLite", "count:" + count);

//把数据写入SD卡目录下
while ((count = inputStream.read(buffer)) > 0 ) {
fileOutputStream.write(buffer, 0, count);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
}

} catch (FileNotFoundException e) {
Log.e("Database", "File not found");
e.printStackTrace();
}
} catch (IOException e) {
Log.e("Database", "IO exception");
e.printStackTrace();
}

}

C. android 获取 安装包 的文件名

亲爱的,我是搞Android开发的,我想告诉你的是,这两个名字,其实并不是在一个地方命名的。
1、也就是说,你下载的那个OK_5462.apk我可以把这个文件重命名。这个.apk的后缀其实是一个压缩文件而已,我可以任意命名,
2、然后里面的应用的名字,我实在Strings的app_name里面命名的,这个是编程的时候做的,你可以理解为这个是内部命名的,第一条是外部命名的。
所以,懂了吗?

D. android 使用URL获取本地的文件

public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}

E. Android:如何通过资源ID接收资源的文件名和扩展名

这样是不行的。
别的方法有,将你的图片放到asset文件夹里,然后你就可以直接访问文件了,不需要任何的授权。
你可以在Activity中:AssetManager
am
=
this.getApplicationContext().getAssets()
InputStream
is
=
am.open(foldername+"/"+filename)
Bitmap
myNewImage
=
BitmapFactory.decodeStream(is);

F. android如何从一个URI当中提取出文件名和后缀名。

首先你获取到路径的字符串,然后分割就行了。
String a = "/test/aaaa/bbb.txt";
String b = a.substring(a.lastIndexOf("/") + 1, a.length());
System.out.println(b);
这样之后,b字符串就是你所需要的。

G. android获取sdcard下的文件名

private ArrayList<String> getFileNameList(File path){
ArrayList<String> FileNameList=new ArrayList<String>();
//如果是文件夹的话
if(path.isDirectory()){
//什么也不做
}
//如果是文件的话直接加入
else{
Log.i(TAG, path.getAbsolutePath());
//进行文件的处理
String filePath = path.getAbsolutePath();
//文件名
String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
//添加
FileNameList.add(fileName);
}
}

}

需要的话直接调用getFileNameList("/sdcard/");就能得到文件名的list了

H. Android如何获取服务器文件夹下的文件名

如果你会java
web的话,很简单的。既然你问出来了,估计你不会Java
web开发。你可以先看看大致了解一下Java的servlet。获取目录下的所有文件,java
File对象提供的有接口
查看原帖>>

I. Android文件重命名renameTo后,getName获取的文件名为何还是原来没有改的名字(文件已经重命名了)

Android 很多东西都是存在 ContentProvoid 中的,你改了之后要通知系统进行刷新才可以;
public void dealwithMediaScanIntentData(String path)
{
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File(path));
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

J. android获取本地文件名字

我写了个例子,你看能用吗?

package com.dragonred.android.utils;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

public final class FileUtils {

public final static String PACKAGE_PATH = "com.dragonred.android";
// public final static String LOG_FILE_NAME = "smartprint.txt";
// public final static String LOG_FILE_PATH = STORE_DIRECTORY_PATH + File.separatorChar + LOG_FILE_NAME;

/**
* read key value from preference by key name
*
* @param context
* @param keyName
* @return
*/
public final static String readPreperence(Context context, String keyName) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0);
return settings.getString(keyName, "");
}

/**
* write key name and key value into preference
*
* @param context
* @param keyName
* @param keyValue
*/
public final static void writePreperence(Context context, String keyName,
String keyValue) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(keyName, keyValue);
editor.commit();
}

/**
* delete key from preference by key name
*
* @param context
* @param keyName
*/
public final static void deletePreperence(Context context, String keyName) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0);
SharedPreferences.Editor editor = settings.edit();

editor.remove(keyName);
editor.commit();
}

public final static String getContextFilePath(Context context, String fileName) {
return context.getFilesDir().getAbsolutePath() + File.separatorChar + fileName;
}

public final static boolean existContextFile(Context context, String fileName) {
String filePath = context.getFilesDir().getAbsolutePath() + File.separatorChar + fileName;
Log.d("filePath", filePath);

File file = new File(filePath);
if (file.exists()) {
return true;
}
return false;
}

public final static void saveContextFile(Context context, String fileName, String content) throws Exception {
FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
}

public final static void saveAppendContextFile(Context context, String fileName, String content) throws Exception {
FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
}

public final static void deleteContextFile(Context context, String fileName) throws Exception {
context.deleteFile(fileName);
}

public final static String readContextFile(Context context, String fileName) throws Exception {
FileInputStream inputStream = context.openFileInput(fileName);
byte[] buffer = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
byte[] data = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
inputStream.close();
return new String(data);
}

/**
* delete file or folders
* @param file
*/
public final static void deleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFile(files[i]);
}
}
file.delete();
} else {
Log.d("deleteFile", "The file or directory does not exist!");
}
}

/**
* make directory on SD card
* @param dirPath
* @return
*/
public final static boolean makeDir(String dirPath) {
File dir = new File(dirPath);
if(!dir.isDirectory()) {
if (dir.mkdirs()) {
return true;
}
} else {
return true;
}
return false;
}

/**
* write log file
* @param filePath
* @param tag
* @param content
*/
public final static void writeLog(String filePath, String tag, String content) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String logDateTime = sdf.format(new Date());
writeFileByAppend(filePath, logDateTime + "---[" + tag + "]---" + content + "\n");
}

/**
* write file by append mode
* @param filePath
* @param content
*/
public final static void writeFileByAppend(String filePath, String content) {
// FileWriter writer = null;
// try {
// writer = new FileWriter(filePath, true);
// writer.write(content);
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// writer.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
RandomAccessFile randomFile = null;
try {
randomFile = new RandomAccessFile(filePath, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.write(content.getBytes());

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// BufferedWriter out = null;
// try {
// out = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream(filePath, true), "UTF-8"));
// out.write(content);
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }

}

/**
* write file by overwrite mode
* @param filePath
* @param content
*/
public final static void writeFile(String filePath, String content) {
// FileWriter writer = null;
// try {
// writer = new FileWriter(filePath, true);
// writer.write(content);
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// writer.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath, false), "UTF-8"));
out.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* check SD card whether or not exist
* @param context
* @return
*/
public final static boolean checkSDCard(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
} else {
// Toast.makeText(context, "Please check your SD card! ",
// Toast.LENGTH_SHORT).show();
return false;
}
}

/**
* read last line from file
* @param filePath
* @return
*/
public final static String readLastLinefromFile(String filePath) {
RandomAccessFile raf = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return null;
}

raf = new RandomAccessFile(filePath, "r");
long len = raf.length();
if (len == 0L) {
return "";
} else {
long pos = len - 1;
while (pos > 0) {
pos--;
raf.seek(pos);
if (raf.readByte() == '\n') {
break;
}
}
if (pos == 0) {
raf.seek(0);
}
byte[] bytes = new byte[(int) (len - pos)];
raf.read(bytes);
return new String(bytes);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (Exception e2) {
}
}
}
return null;
}
}

热点内容
明锐pro为什么比高尔夫配置还要高 发布:2025-03-22 00:24:43 浏览:129
卖房解压担保 发布:2025-03-22 00:18:57 浏览:450
java打开页面 发布:2025-03-22 00:18:41 浏览:448
mt4ea源码 发布:2025-03-21 23:59:08 浏览:532
文件夹加密隐藏 发布:2025-03-21 23:56:24 浏览:17
setjava用法 发布:2025-03-21 23:54:59 浏览:182
spring配置的主要标签有哪些 发布:2025-03-21 23:54:57 浏览:174
python3range 发布:2025-03-21 23:42:56 浏览:347
安卓国外手机在哪个平台买 发布:2025-03-21 23:39:40 浏览:118
androidx86卡 发布:2025-03-21 23:38:06 浏览:805