如何将文件复制到android的文件系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2668282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:51:36  来源:igfitidea点击:

how can I copy files to the file system of android?

androidfilesystems

提问by Alex Kapustian

How can I copy files to the file system (place) in android? How can I access it?

如何将文件复制到android中的文件系统(地方)?我怎样才能访问它?

回答by Raja

copy files from android to local

将文件从android复制到本地

adb pull /data/data/com.example.sample ./sample/

copy file to android from local

将文件从本地复制到android

adb push ./sample/ /sdcard/sample/

回答by Chris Thompson

I'm not sure if this is what you're asking, but here's another interpretation of your question:

我不确定这是否是您要问的问题,但这是对您问题的另一种解释:

You can place files in the assetsfolder in the project on your development machine. They will be bundled into the apk file and then access them through the AssetManagerclass. Something like this from within a Service or Activity class:

您可以将文件assets放在开发机器上项目的文件夹中。它们将被捆绑到 apk 文件中,然后通过AssetManager类访问它们。来自 Service 或 Activity 类的类似内容:

AssetManager am = getAssets();
InputStream is = am.open("filename");

This will allow you to read the contents of those files. I don't believe you can write to them though. There are other options for if you need to read and write files in your application's storage sandbox. This is done by calling openFileOutput(). I should note though that the user can always access the files on the device through ddms but other applications won't be able to access files stored in your application's storage sandbox.

这将允许您阅读这些文件的内容。我不相信你可以写信给他们。如果您需要在应用程序的存储沙箱中读取和写入文件,还有其他选项。这是通过调用openFileOutput(). 我应该注意的是,用户始终可以通过 ddms 访问设备上的文件,但其他应用程序将无法访问存储在您的应用程序存储沙箱中的文件。

EditBased on your comment, you probably want to use

编辑根据您的评论,您可能想要使用

OutputStream out = openFileOutput("outfile");

to write to a file from your code. This code must be called from within an Activityor Service(something that extends Context). This file will be stored on the phone's file system and not on the sdcard. These files will not be accessible to the average user. If users know how to enable usb debugging then they will be able to access them and there is really no way around this.

从您的代码写入文件。必须从Activityor Service(扩展的东西Context)中调用此代码。此文件将存储在手机的文件系统中,而不是存储在 SD 卡上。普通用户将无法访问这些文件。如果用户知道如何启用 USB 调试,那么他们将能够访问它们,并且真的没有办法解决这个问题。

回答by Danh Nguyen

I took some time to research about android's system files earlier, but almost always with android x86, i found out that the /system folder inside android is read-only,

我之前花了一些时间研究android的系统文件,但几乎总是使用android x86,我发现android中的/system文件夹是只读的,

it is an image with format is SFS format, this is read-only image format of linux so i don't think you can copy some thing inside this folder. I not sure about rooted android. Hope it will help you.

它是一个格式为 SFS 格式的图像,这是 linux 的只读图像格式,所以我认为你不能在这个文件夹中复制一些东西。我不确定是否有 root 的 android。希望它会帮助你。

回答by Macarse

You can use:

您可以使用:

Environment.getExternalStorageDirectory()

That will give you /sdcardif your device has an sdcard, or something different in other cases. For example the Archos.

/sdcard如果您的设备有 SD 卡,或者在其他情况下有不同的东西,这将为您提供。例如爱可视。

回答by Rorist

You can probably hide your file in the sdcard by prefixing it with a dot (ex: .myfile)

您可以通过在 sdcard 前面加上一个点来隐藏您的文件(例如:.myfile)