2

DownloadsSDカードがあるデバイスとないデバイスのフォルダのデフォルトの場所を教えてください。

また、特定の電話にSDカードがないかどうかを確認するにはどうすればよいですか。

4

2 に答える 2

10

デバイスにSDカードがあるかどうかを確認するには、次を使用しますEnvironment.getExternalStorageState()。SDカードがない場合は、次を使用します。Environment.getDataDirectory()

基本的に、SDカードがない場合は、デバイス上にローカルで独自のディレクトリを作成できます。私は役立つかもしれないいくつかのコードをリストしました:

/*
             * This sections checks the phone to see if there is a SD card. if
             * there is an SD card, a directory is created on the SD card to
             * store the test log results. If there is not a SD card, then the
             * directory is created on the phones internal hard drive
             */
                    //if there is no SD card, create new directory objects to make directory on device
            if (Environment.getExternalStorageState() == null) {
                            //create new file directory object
                directory = new File(Environment.getDataDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(Environment.getDataDirectory()
                        + "/Robotium-Screenshots/");
                /*
                 * this checks to see if there are any previous test photo files
                 * if there are any photos, they are deleted for the sake of
                 * memory
                 */
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length != 0) {
                        for (int ii = 0; ii <= dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                    }
                }
                // if no directory exists, create new directory
                if (!directory.exists()) {
                    directory.mkdir();
                }

                // if phone DOES have sd card
            } else if (Environment.getExternalStorageState() != null) {
                // search for directory on SD card
                directory = new File(Environment.getExternalStorageDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(
                        Environment.getExternalStorageDirectory()
                                + "/Robotium-Screenshots/");
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length > 0) {
                        for (int ii = 0; ii < dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                        dirFiles = null;
                    }
                }
                // if no directory exists, create new directory to store test
                // results
                if (!directory.exists()) {
                    directory.mkdir();
                }
            }// end of SD card checking

お役に立てれば。

于 2012-07-05T20:13:48.893 に答える
1

内部のダウンロード場所がわかりません。

プログラムで外部ダウンロード場所を取得するには、次を使用します。

Environment.getExternalStorageDirectory()+"/Downloads/"

または、少なくとも、それが私のHTCEVO4Gの機能です。

私のアプリでは、SDカードの現在の状態を検出し、人間が理解できる用語に簡略化した単純なクラスを開発しました。それはを使用して簡単に行うことができます

Environment.getExternalStorageState()

Environmentそして、それをクラスの下で定義されたさまざまなデフォルト状態と比較します。重要なのはEnvironment.MEDIA_REMOVEDEnvironment.MEDIA_SHAREDです。最初の(私は信じています)は、外部ストレージがあるかどうかを示し、2番目のストレージはマウントされているかどうかを示します。

注:状態を比較するときは、Androidのほとんどの状態オブジェクトのように整数ではなく、文字列である.equalsを使用してください。

于 2012-07-05T20:19:58.317 に答える