如何访问我在外部创建的文件?也就是文件在哪里?
How can I access a file that I have created externally? AKA where is the file?
我正在尝试为 SQLite 数据库应用创建 backup/restore (export/import) 进程。
虽然我似乎已经创建并填充了文件(好的,我现在知道我已经创建了)。我在 DDMS 和 Windows Explorer 中都看不到该文件。我真的很想能够做到后者(请参阅底部以获得更具体的问题)。
我已经成功写入文件并使用以下代码读取文件:
package mjt.sqlitetutorial;
import android.database.Cursor; //+++++ Added
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; //+++++ Added
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
public int API_VERSION = Build.VERSION.SDK_INT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (API_VERSION >= 23) {
ExternalStoragePermissions.verifyStoragePermissions(this);
}
final String EXTSTGTAG = "EXTERNSTG";
File file = getExternalFilesDir("File");
Log.i(EXTSTGTAG,file.toString());
//String extstgdirabs = Environment.getExternalStorageDirectory().getAbsolutePath();
String extstgdirpth = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
Log.i(EXTSTGTAG,"<=\nEXTERN STG PUB DIR=>" + extstgdirpth);
String filepath = extstgdirpth + File.separator + "myfile.txt";
Log.i(EXTSTGTAG,"Full File Path and name is\n\t" + filepath);
File f = new File(filepath);
if(!f.exists() ) {
Log.i(EXTSTGTAG,"File did not exist (path=" + filepath + ")");
try {
f.createNewFile();
}
catch (IOException e) {
Log.e(EXTSTGTAG,"Failure Creating New File MSG=" + e.getMessage());
}
}
if(f.exists()) {
Log.i(EXTSTGTAG,"File Already Exists (" + filepath + ")");
try {
Log.i(EXTSTGTAG,"Creating FileOutputStream instance.");
FileOutputStream fos = new FileOutputStream(f);
Log.i(EXTSTGTAG,"Creating OutputStreamWriter instance from FileOutputStream.");
OutputStreamWriter osw = new OutputStreamWriter(fos);
Log.i(EXTSTGTAG,"Adding Data to OutputStreamWriter.");
osw.append("My Test Data.");
Log.i(EXTSTGTAG,"Closing OutputStreamWriter.");
osw.close();
Log.i(EXTSTGTAG,"Flushing FileOutputStream.");
fos.flush();
Log.i(EXTSTGTAG,"Closing FileOutputStream");
fos.close();
Log.i(EXTSTGTAG,"All Done OK.");
} catch (IOException e) {
Log.e(EXTSTGTAG, "Failure Trying to write to file." + e.getMessage());
e.printStackTrace();
}
} else {
Log.i(EXTSTGTAG,"File doesn't appear to exist when it should????");
}
f.setReadable(true);
f.setWritable(true);
if(f.exists()) {
try {
byte[] bytes;
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
Log.i(EXTSTGTAG,"Read the following data:\n" + sb.toString());
}
catch (IOException e) {
Log.e(EXTSTGTAG,"Failure trying to read file." + e.getMessage());
e.printStackTrace();
}
}
}
}
日志输出(使用 EXTERN 作为过滤器)显示(注意第一个 运行 安装应用程序时 运行 失败但请求并设置权限。我不相信这是一个issue/cause) :-
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: /storage/emulated/0/Android/data/mjt.sqlitetutorial/files/File
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: <=
EXTERN STG PUB DIR=>/storage/emulated/0/Download
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Full File Path and name is
/storage/emulated/0/Download/myfile.txt
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: File Already Exists (/storage/emulated/0/Download/myfile.txt)
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Creating FileOutputStream instance.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Creating OutputStreamWriter instance from FileOutputStream.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Adding Data to OutputStreamWriter.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Closing OutputStreamWriter.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Flushing FileOutputStream.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Closing FileOutputStream
10-18 12:54:40.169 32393-32393/? I/EXTERNSTG: All Done OK.
10-18 12:54:40.169 32393-32393/? I/EXTERNSTG: Read the following data:
My Test Data.
最后一行表明它已读取文件(我假设)。在提供的消息期间没有其他日志消息(虽然之前和之后有很多)。
我正在测试的设备是带有 SD 卡的无根 HTC One M8s。但是,我认为/storage/emulated/0/Download,写入文件的目录在内存中。
使用 DDMS 我似乎看不到这个(实际的 SD 卡有一个 Downloads 目录,而不是 Download 目录)。
在 Windows 资源管理器中,我可以看到 Internal Storage 和 SD 卡 作为 下的设备HTC_0PKV1 设备。
在 Windows Explorer 中,Download 目录有(通过属性)0 个目录和文件。只读和隐藏都没有勾选
我试过使用和不使用 setReadable
和 setWritable()
。
我刚刚尝试使用 phone 上的文件管理器,现在可以看到该文件。更具体地说,问题是; 有什么方法可以排除 phone 的 root 权限和通过 phone 上的文件管理器移动文件以通过 Windows Explorer 访问文件?
我还应该说明该应用程序将 运行 在平板电脑上使用,因此该方法应该是通用的而不是特定于设备的。
断开并重新连接 USB 数据线后,文件在 Windows 资源管理器中可见。我不确定 MTP 是如何工作的,还是可能是由于 ADB 根据这个片段:-
However, if you’ve ever attempted to unlock your device such as to
install a new ROM or root it, then you may have at one time or another
installed the Android Debug Bridge (ADB) driver on your computer. This
driver works great for being able to use the computer to send commands
to your device, but it may mess up your easy-peasy file manipulation.
发现于 How to Get Your Android Device to Show up in File Explorer (If It Isn’t)
我正在尝试为 SQLite 数据库应用创建 backup/restore (export/import) 进程。
虽然我似乎已经创建并填充了文件(好的,我现在知道我已经创建了)。我在 DDMS 和 Windows Explorer 中都看不到该文件。我真的很想能够做到后者(请参阅底部以获得更具体的问题)。
我已经成功写入文件并使用以下代码读取文件:
package mjt.sqlitetutorial;
import android.database.Cursor; //+++++ Added
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; //+++++ Added
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
public int API_VERSION = Build.VERSION.SDK_INT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (API_VERSION >= 23) {
ExternalStoragePermissions.verifyStoragePermissions(this);
}
final String EXTSTGTAG = "EXTERNSTG";
File file = getExternalFilesDir("File");
Log.i(EXTSTGTAG,file.toString());
//String extstgdirabs = Environment.getExternalStorageDirectory().getAbsolutePath();
String extstgdirpth = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
Log.i(EXTSTGTAG,"<=\nEXTERN STG PUB DIR=>" + extstgdirpth);
String filepath = extstgdirpth + File.separator + "myfile.txt";
Log.i(EXTSTGTAG,"Full File Path and name is\n\t" + filepath);
File f = new File(filepath);
if(!f.exists() ) {
Log.i(EXTSTGTAG,"File did not exist (path=" + filepath + ")");
try {
f.createNewFile();
}
catch (IOException e) {
Log.e(EXTSTGTAG,"Failure Creating New File MSG=" + e.getMessage());
}
}
if(f.exists()) {
Log.i(EXTSTGTAG,"File Already Exists (" + filepath + ")");
try {
Log.i(EXTSTGTAG,"Creating FileOutputStream instance.");
FileOutputStream fos = new FileOutputStream(f);
Log.i(EXTSTGTAG,"Creating OutputStreamWriter instance from FileOutputStream.");
OutputStreamWriter osw = new OutputStreamWriter(fos);
Log.i(EXTSTGTAG,"Adding Data to OutputStreamWriter.");
osw.append("My Test Data.");
Log.i(EXTSTGTAG,"Closing OutputStreamWriter.");
osw.close();
Log.i(EXTSTGTAG,"Flushing FileOutputStream.");
fos.flush();
Log.i(EXTSTGTAG,"Closing FileOutputStream");
fos.close();
Log.i(EXTSTGTAG,"All Done OK.");
} catch (IOException e) {
Log.e(EXTSTGTAG, "Failure Trying to write to file." + e.getMessage());
e.printStackTrace();
}
} else {
Log.i(EXTSTGTAG,"File doesn't appear to exist when it should????");
}
f.setReadable(true);
f.setWritable(true);
if(f.exists()) {
try {
byte[] bytes;
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
Log.i(EXTSTGTAG,"Read the following data:\n" + sb.toString());
}
catch (IOException e) {
Log.e(EXTSTGTAG,"Failure trying to read file." + e.getMessage());
e.printStackTrace();
}
}
}
}
日志输出(使用 EXTERN 作为过滤器)显示(注意第一个 运行 安装应用程序时 运行 失败但请求并设置权限。我不相信这是一个issue/cause) :-
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: /storage/emulated/0/Android/data/mjt.sqlitetutorial/files/File
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: <=
EXTERN STG PUB DIR=>/storage/emulated/0/Download
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Full File Path and name is
/storage/emulated/0/Download/myfile.txt
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: File Already Exists (/storage/emulated/0/Download/myfile.txt)
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Creating FileOutputStream instance.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Creating OutputStreamWriter instance from FileOutputStream.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Adding Data to OutputStreamWriter.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Closing OutputStreamWriter.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Flushing FileOutputStream.
10-18 12:54:40.159 32393-32393/? I/EXTERNSTG: Closing FileOutputStream
10-18 12:54:40.169 32393-32393/? I/EXTERNSTG: All Done OK.
10-18 12:54:40.169 32393-32393/? I/EXTERNSTG: Read the following data:
My Test Data.
最后一行表明它已读取文件(我假设)。在提供的消息期间没有其他日志消息(虽然之前和之后有很多)。
我正在测试的设备是带有 SD 卡的无根 HTC One M8s。但是,我认为/storage/emulated/0/Download,写入文件的目录在内存中。
使用 DDMS 我似乎看不到这个(实际的 SD 卡有一个 Downloads 目录,而不是 Download 目录)。
在 Windows 资源管理器中,我可以看到 Internal Storage 和 SD 卡 作为 下的设备HTC_0PKV1 设备。
在 Windows Explorer 中,Download 目录有(通过属性)0 个目录和文件。只读和隐藏都没有勾选
我试过使用和不使用 setReadable
和 setWritable()
。
我刚刚尝试使用 phone 上的文件管理器,现在可以看到该文件。更具体地说,问题是; 有什么方法可以排除 phone 的 root 权限和通过 phone 上的文件管理器移动文件以通过 Windows Explorer 访问文件?
我还应该说明该应用程序将 运行 在平板电脑上使用,因此该方法应该是通用的而不是特定于设备的。
断开并重新连接 USB 数据线后,文件在 Windows 资源管理器中可见。我不确定 MTP 是如何工作的,还是可能是由于 ADB 根据这个片段:-
However, if you’ve ever attempted to unlock your device such as to install a new ROM or root it, then you may have at one time or another installed the Android Debug Bridge (ADB) driver on your computer. This driver works great for being able to use the computer to send commands to your device, but it may mess up your easy-peasy file manipulation.
发现于 How to Get Your Android Device to Show up in File Explorer (If It Isn’t)