IPL*_*L10 1 android sd-card android-sdcard
我想在一个文件夹中的SD卡上备份我的收件箱短信.但我无法在SD卡上制作文件夹我正在使用此代码
backup=(Button)findViewById(R.id.backup);
backup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
backupSMS();
}
public ArrayList<String> smsBuffer = new ArrayList<String>();
String smsFile = "SMS-" + SystemClock.currentThreadTimeMillis() + ".csv";
private void backupSMS() {
smsBuffer.clear();
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(
mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);
//startManagingCursor(cursor1);
String[] columns = new String[] { "_id", "thread_id", "address", "person", "date", "body",
"type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
Log.d("Count",count);
while (cursor1.moveToNext()) {
String messageId = cursor1.getString(cursor1
.getColumnIndex(columns[0]));
String threadId = cursor1.getString(cursor1
.getColumnIndex(columns[1]));
String address = cursor1.getString(cursor1
.getColumnIndex(columns[2]));
String name = cursor1.getString(cursor1
.getColumnIndex(columns[3]));
String date = cursor1.getString(cursor1
.getColumnIndex(columns[4]));
String msg = cursor1.getString(cursor1
.getColumnIndex(columns[5]));
String type = cursor1.getString(cursor1
.getColumnIndex(columns[6]));
smsBuffer.add(messageId + ","+ threadId+ ","+ address + "," + name + "," + date + " ," + msg + " ,"
+ type);
}
generateCSVFileForSMS(smsBuffer);
}
}
private void generateCSVFileForSMS(ArrayList<String> list)
{
try
{
String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + smsFile;
FileWriter write = new FileWriter(storage_path);
write.append("messageId, threadId, Address, Name, Date, msg, type");
write.append('\n');
write.append('\n');
for (String s : list)
{
write.append(s);
write.append('\n');
}
write.flush();
write.close();
}
catch (NullPointerException e)
{
System.out.println("Nullpointer Exception "+e);
// e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
Run Code Online (Sandbox Code Playgroud)
使用此代码我可以备份但不能在文件夹中.请帮助我,我是新来的android,感谢提前
还要检查是否已安装SD卡
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
// sd card mounted
}
File direct = new File(Environment.getExternalStorageDirectory() + "/YourFolder");
if(!direct.exists())
{
if(direct.mkdir())
{
//directory is created;
}
}
Run Code Online (Sandbox Code Playgroud)
我忘了提到你需要在清单中提供许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
使用上述权限,您将默认具有读取权限.
也最好用File.seperator而不是/
小智 6
如果您创建一个包装顶级目录的File对象,您可以调用它的mkdirs()方法来构建所有需要的目录.就像是:
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
Run Code Online (Sandbox Code Playgroud)
注意:使用Environment.getExternalStorageDirectory()来获取"SD卡"目录可能是明智之举,因为如果手机出现了除SD卡以外的其他东西(例如内置闪存,则可能会更改)苹果手机).无论哪种方式,您都应该记住,您需要检查以确保它实际存在,因为可能会移除SD卡.
更新:自API级别4(1.6)起,您还必须请求许可.这样的事情(在清单中)应该有效:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9497 次 |
| 最近记录: |