0

チュートリアルからこのコードを見つけましたが、機能しません。

ディレクトリを作成できないと言っています

    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();
4

6 に答える 6

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" />
于 2012-12-27T12:45:26.140 に答える
1

おそらく、マニフェストにアクセス許可を追加する必要があります。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-27T12:42:31.030 に答える
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));
}
于 2013-12-16T05:24:57.027 に答える
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
    }

}

-プリーヤ

于 2012-12-27T12:47:03.020 に答える
0

これを試して :)

String path=Environment.getExternalStorageDirectory().getAbsolutePath();
File directory = new File(path+"your directory");

そして、マニフェストの許可を確認してください

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-27T12:43:56.010 に答える
0

私の場合、次の方法を使用して、データベースパスが間違っている可能性があると思います:

    public static String PRIVATE_DATA = "/data/data/<your package name>/databases/<your db name>";

また、androidManifest.xml に外部ストレージ デバイスへの SD カード書き込みのアクセス許可を追加します。

于 2012-12-27T12:44:11.050 に答える