當前位置:首頁 » 安卓系統 » 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;
}
}

熱點內容
ajax跨域訪問wcf 發布:2025-01-24 21:08:21 瀏覽:662
iphonecpp編譯器 發布:2025-01-24 21:05:52 瀏覽:201
androidsdk接入 發布:2025-01-24 20:54:14 瀏覽:193
我的世界伺服器如何使用路由器映射 發布:2025-01-24 20:49:30 瀏覽:739
腳本操作瀏覽器 發布:2025-01-24 20:41:40 瀏覽:296
fast自動獲取ip地址伺服器無響應 發布:2025-01-24 20:19:13 瀏覽:710
http加密數據 發布:2025-01-24 20:15:00 瀏覽:100
中國存儲行業排名 發布:2025-01-24 20:02:21 瀏覽:422
arm編譯鏈 發布:2025-01-24 19:42:12 瀏覽:700
linuxc的函數返回值 發布:2025-01-24 19:35:23 瀏覽:665