我正在使用以下代码写入文件:
File file = new File(getCacheDir(), "cachefile");
FileOutputStream fos = new FileOutputStream(file);
StringBuilder cachetext = new StringBuilder();
Iterator bri = brands.iterator();
Iterator bli = brand_id.iterator();
while(bli.hasNext()) {
cachetext.append(bli.next() + "|" + bri.next() + System.getProperty("line.separator"));
}
fos.write(cachetext.toString().getBytes());
fos.close();
Run Code Online (Sandbox Code Playgroud)
这很好 - 没有错误,文件最终包含我希望它包含的内容.但是,当我通过openFileInput()读取它时,我得到一个异常,告诉我不允许路径分隔符
FileInputStream fis = openFileInput(getCacheDir() + "/cachefile");
Run Code Online (Sandbox Code Playgroud)
很公平,它包含一个斜杠,但我怎么能指定我想要打开的文件的路径?当然,必须有办法做到这一点,但我无法通过谷歌找到答案("阅读","缓存"和"文件"不是最常用的术语......)所以我想我会尝试人性化.提前致谢!
我正在尝试在android中的一个文本文件中写一个新行.
这是我的代码:
FileOutputStream fOut;
try {
String newline = "\r\n";
fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.write(newline);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我试过\n,\r\n我也尝试获取新行的系统属性,它们都不起作用.
数据变量包含来自同一文件的先前数据.
String data = "";
try {
FileInputStream in = openFileInput("cache.txt");
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
BufferedReader inRd = new BufferedReader(isr,8 * 1024);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text); …Run Code Online (Sandbox Code Playgroud) 我想在用户单击按钮时更新assets文件夹中的文件.
我的听众是经典
OnClickListener myUpdateListener = new OnClickListener() {
public void onClick(View v) {
updateAllParams();
}
};
Run Code Online (Sandbox Code Playgroud)
新文件是以这种形式的网址
http://sample.test/android/params.dtf
在文件资产文件夹
params.dtf
Run Code Online (Sandbox Code Playgroud)
但我不知道如何用网站上的新版本替换此文件.
android中的内部存储和外部存储是什么意思??s 外部存储微型 SD 卡和内部存储手机内存?如果我需要为我的应用程序编写一个文件,这只是我的应用程序需要的(用户与这个文件无关),那么我应该在哪里写我的文件 - 在内部存储器还是外部存储器中?
我认为,如果我将文件写入 SD 卡(外部存储器),那么当用户拔出 SD 卡时,应用程序将无法读取该文件。这样对吗?如果是正确的,那么在外部存储器中写入文件将不符合我的标准。因为,我的应用程序必须多次检查文件中的某些数据,如果在那个特定时刻用户拔出 SD 卡,我的应用程序将无法实现其目标。
而且我需要永久写入文件(但如果用户卸载该应用程序,则应删除该文件)。那么,我应该走哪条路?
我在我的应用程序中实现缓存并在4.2.x设备上部署它时遇到问题.
当我在app cache目录中保存文件时,我无法读取它.我使用具有root权限的文件管理器来浏览缓存文件夹,并且文件正确地存在于我的缓存文件夹中,但是当我尝试读取它们时,获取文件未找到错误.
此问题仅与4.2 android版本有关.在4.1-一切正常
这是我用来编写文件的代码
public static void writeFile(Bitmap bmp, File f) {
if (bmp == null)
return;
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
boolean res = bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (Exception ex) {
}
}
f.setReadable(true, false);
}
Run Code Online (Sandbox Code Playgroud)
这就是我构造文件对象的方式
File bitmapFile = new File(ImageManager.getInstance().getCacheDir(), key);
Run Code Online (Sandbox Code Playgroud)
这是如何获取缓存路径
if (Config.USE_INTERNAL_CACHE == false && sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我需要用户能够打开我的应用程序,例如当从 gmail 应用程序、下载文件夹或“文件资源管理器”单击附件时。
我已经在谷歌上搜索了很多,甚至在 SO 中发现了一些问题,但我无法完成这项工作。
这是我的 Manifest.xml 文件的样子:
<receiver android:name="MySMS2CloudReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_VIEW" />
<action android:name="android.intent.action.ACTION_EDIT" />
<action android:name="android.intent.action.ACTION_PICK" />
<data android:scheme="http" />
<data android:pathPattern=".*\\.xml" />
<data android:host="*" />
<data android:mimeType="application/rss+xml" />
<data android:mimeType="application/atom+xml" />
<data android:mimeType="application/xml" />
<data android:mimeType="text/xml" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
我的广播接收器:
public class MySMS2CloudReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MySMS2CloudReceiver", "Leaving onReceived...");
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在,当我在弹出的标题为“使用完成操作”的电子邮件中单击附件时,我的应用程序甚至没有列在那里......
知道可能会发生什么吗?
谢谢!!
在我的应用程序中,我将一个特定的联系人项目备份为.vcf文件,它工作正常,但我需要在不使用外部应用程序的情况下恢复联系人
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard"); //storage
startActivity(intent);
Run Code Online (Sandbox Code Playgroud) 我试图获取文件/sdcard/夹中的文件,但它抛出NullPointerException
然后我试图获取/路径中的文件夹它返回
40
factory
usbdisk
sdcard
storage
config
cache
acct
vendor
d
etc
mnt
ueventd.tuna.rc
ueventd.rc
ueventd.goldfish.rc
system
sys
sepolicy
seapp_contexts
sbin
res
... and others
Run Code Online (Sandbox Code Playgroud)
当我在4.3项目中这样做时,它运作良好!
UPDATE
4.4中的代码不起作用
File file = Environment.getExternalStorageDirectory();
File[] files = file.listFiles();
Debugger.info(files.length); // NullPointerExcepton
for(File f : files){
Debugger.info(f.getName());
}
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中实现了一个文件选择器,如下所示:
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Title"), FILE_PICK);
Run Code Online (Sandbox Code Playgroud)
这是一个已知问题,您无法通过这种方式轻松获取实际文件位置,因为 Intent 会返回一些您无法真正用于创建 File 对象的奇怪 Uri。
我正在使用这种方法来获取实际的文件路径:
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
public static String getPath(final Context context, final Uri uri) { …Run Code Online (Sandbox Code Playgroud) 我尝试了很多方法,但我不能这样做.
我有一个*.txt文件.我希望通过它分享Bluetooth , wifi , email and ....
当我使用此代码时,我无法共享该文件:
File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/txt");
sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
startActivity(Intent.createChooser(sharingIntent, "share file with"));
Run Code Online (Sandbox Code Playgroud)
我完全想要这样:当用户点击共享按钮并选择一个电子邮件发件人(如Gmail)进行共享时.该文件必须附加新文件的文件...
我找到了这个链接/sf/answers/1122537461/
但它共享txt文件内容.我想共享文件而不是txt文件的内容