我从教程中找到了这段代码,但它没有用。
说目录无法创建
try {
myInput = new FileInputStream("//data//net.learn2develop.Databases//databases//MyDB");//this is
// the path for all apps
//insert your package instead packagename,ex:com.mybusiness.myapp
// Set the output folder on the SDcard
// File directory = new File(Environment.getExternalStorageDirectory().getPath());
File directory = new File("/sdcard/some_folder");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
// Set the output file stream up:
// OutputStream myOutput = new FileOutputStream("/sdcard/");
OutputStream myOutput = new FileOutputStream(directory.getPath()+
"/database_name.backup");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
答案 0 :(得分:2)
这是错误的
"//data//net.learn2develop.Databases//databases//MyDB"
应为"/data/data/net.learn2develop.Databases/databases/MyDB"
此外,永远不要在路径中编码"sdcard"
字符串
您应该按如下方式获取输出文件:
File directory = new File(Environment.getExternalStorageDirectory(), "directory_name");
directory.makeDirs();
另请在清单文件中检查您的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
可能是,您需要向Manifest添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
试试这个:)
String path=Environment.getExternalStorageDirectory().getAbsolutePath();
File directory = new File(path+"your directory");
并检查清单中的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 3 :(得分:0)
我认为你的数据库路径可能是错误的,在我的情况下使用以下方式:
public static String PRIVATE_DATA = "/data/data/<your package name>/databases/<your db name>";
还在androidManifest.xml中添加sd卡写入外部存储设备的权限
答案 4 :(得分:0)
以下代码适用于我:) :) ..希望它也可以帮到你。
public class FileDownloader implements IJuicerDownloader {
public static final String tag = "FileDownloader";
public void download(String downloadFileWebPath, File savedFilename ) throws Exception
{
try
{
URL myFileUrl =null;
myFileUrl= new URL(downloadFileWebPath);
Log.d(tag,"downloadFileWebPath " + downloadFileWebPath );
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream downloadFile = conn.getInputStream();
int bytes = downloadFile.available();
FileOutputStream fos = new FileOutputStream(savedFilename);
BufferedInputStream bis = new BufferedInputStream(downloadFile);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
fos.write(baf.toByteArray());
fos.flush();
fos.close();
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// throw your own exception
}
catch (FileNotFoundException e) {
e.printStackTrace();
// throw your own exception
}catch ( UnknownServiceException e) {
e.printStackTrace();
// throw your own exception
} catch (IOException e) {
e.printStackTrace();
// throw your own exception
}
}
-Preeya
答案 5 :(得分:0)
这里有很多有用的答案。我只是想举一个如何做到这一点的例子。
private final static String EXTERNAL_DIRECTORY = "/Android/data/{Name-Space}/databases/";
private final static String INTERNAL_DIRECTORY = "/data/data/com.fitocracy.app/databases/";
public static void copyDatabaseToSdcard() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = getFIS();
os = getFOS();
int current = 0;
while ((current = is.read()) != -1) {
os.write(current);
}
os.flush();
os.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static BufferedInputStream getFIS() throws FileNotFoundException {
File db = new File(INTERNAL_DIRECTORY + DATABASE_NAME);
return new BufferedInputStream(new FileInputStream(db));
}
private static BufferedOutputStream getFOS() throws FileNotFoundException {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(path + EXTERNAL_DIRECTORY);
if (!dir.exists()) {
dir.mkdirs();
}
File db = new File(dir, DATABASE_NAME);
return new BufferedOutputStream(new FileOutputStream(db));
}