7

答えを見つけるためにウェブを徹底的に検索しましたが、結果はありませんでした。

任意の場所にファイルを保存する機能など、Android アプリにいくつかの「設定」を実装しました。

いわゆる「統合 SD カード」のパスを選択すると、すべて問題ありません。しかし、私は「実際の」外部SDカードも持っています。これは(私の場合)およびにシンボリックリンクされています(「内部」/storage/sdcard1SDカードは/extSdCardおよびへのシンボリックリンクがあります)。/mnt/extSdCard/storage/sdcard0/sdcard/mnt/sdcard

ICS では、external at に/emmc加えて、覚えていないリンクがいくつかありました。

問題は、これを指すパスを選択するextSdCardと、アプリはフォルダー構造を作成しますが、ダウンロードしたファイルを書き込まず、「SecurityException」「destination must be on external storage」で終わることです。

しかし、このパスは外部ストレージにあります! 詳細: 書き込み権限に問題がある場合、なぜフォルダーが作成されるのですか? (android.permission.WRITE_EXTERNAL_STORAGEマニフェストに存在します)。

私が何か間違ったことをした可能性が非常に高いです。または多分いくつかのバグ?

日食ログ:

11-30 11:58:29.143: D/ShareActivity(24752): doInBackground...
11-30 11:58:33.813: D/ShareActivity(24752): The response is: 200
11-30 11:58:50.283: D/ShareActivity(24752): location: Downloads
11-30 11:58:50.443: D/ShareActivity(24752): User defined folders created
11-30 11:58:50.443: D/ShareActivity(24752): path: /storage/sdcard1/temp
11-30 11:59:07.053: D/ShareActivity(24752): downloadUri: file:/storage/sdcard1/temp/test.3gpp
11-30 11:59:07.313: D/AndroidRuntime(24752): Shutting down VM
11-30 11:59:07.318: W/dalvikvm(24752): threadid=1: thread exiting with uncaught exception (group=0x41ce3300)
11-30 11:59:07.318: E/AndroidRuntime(24752): FATAL EXCEPTION: main
11-30 11:59:07.318: E/AndroidRuntime(24752): java.lang.SecurityException: Destination must be on external storage: file:/storage/sdcard1/temp/test.3gpp
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.os.Parcel.readException(Parcel.java:1425)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.content.ContentResolver.insert(ContentResolver.java:864)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.app.DownloadManager.enqueue(DownloadManager.java:904)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at dentex.youtube.downloader.ShareActivity$AsyncDownload$1$1.onClick(ShareActivity.java:269)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.os.Looper.loop(Looper.java:137)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at android.app.ActivityThread.main(ActivityThread.java:4931)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-30 11:59:07.318: E/AndroidRuntime(24752):    at dalvik.system.NativeStart.main(Native Method)
11-30 11:59:09.248: I/Process(24752): Sending signal. PID: 24752 SIG: 9

関連コード:

lv.setOnItemClickListener(new OnItemClickListener() {

            private File userFolder;

                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String location = settings.getString("download_locations", "Downloads");
                Log.d(DEBUG_TAG, "location: " + location);

                boolean userLocationEnabled = settings.getBoolean("enable_user_location", false);

                if (userLocationEnabled == false) {
                    if (location.equals("DCIM") == true) {
                        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                    }
                    if (location.equals("Movies") == true) {
                        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
                    } 
                    if (location.equals("Downloads") == true) {
                        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    }
                    Log.d(DEBUG_TAG, "path: " + path);
                } else {
                        userFolder = new File(settings.getString("user_location", ""));
                        path = userFolder;
                }

                Log.d(DEBUG_TAG, "path: " + path.toString());

                pos = position;
                AlertDialog.Builder helpBuilder = new AlertDialog.Builder(ShareActivity.this);

                helpBuilder.setIcon(android.R.drawable.ic_dialog_info);
                helpBuilder.setTitle("Confirm Download for:");
                helpBuilder.setMessage(" *** msg *** ");

                helpBuilder.setPositiveButton("Download here", new DialogInterface.OnClickListener() {

                    @TargetApi(11)
                    public void onClick(DialogInterface dialog, int which) {
                        mLink = links[pos];
                        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

                        Request request = new Request(Uri.parse(mLink));
                        uri = Uri.parse(path.toURI() + title + "." + mExt);
                        Log.d(DEBUG_TAG, "downloadUri: " + uri);
                        request.setDestinationUri(uri);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        }

                        if (isExternalStorageWritable() == true) {
                            enqueue = downloadManager.enqueue(request);
                        }
                    }
                });

                helpBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   //...
                });

                AlertDialog helpDialog = helpBuilder.create();
                helpDialog.show();

            }
});
4

1 に答える 1

10

Jeremy Meiss ( jerdog )によるTHIS ARTICLEおよびChainfireによるTHIS LINKによると、この動作は当分の間「正常」であるはずです。WRITE_MEDIA_STORAGE 権限が必要ですが、これはシステム アプリにのみ付与されます。

回避策として、私のアクティビティでは、これを次SecurityExceptionのように処理します (疑似コードで):

Intent intent = new Intent(MyActivity.this, DownloadsService.class);

try {
    intent.putExtra("COPY", false);
    enqueue = dm.enqueue(request);
    Log.d(DEBUG_TAG, "_ID " + enqueue + " enqueued");
} catch (SecurityException e) {
    Log.w(DEBUG_TAG, e.getMessage());

    // handle the path on etxSdCard here:
    showSomeInfo();
    intent.putExtra("COPY", true);
    tempDownloadToSdcard(request);
}

startService(intent);

DownloadsService完成したダウンロードを処理する BroadcastReceiver があります。その中でonStartCommand、ブール値のエクストラを次のように取得します。

doCopy = intent.getBooleanExtra("COPY", false);

そして受信機の内部:

if (doCopy) copyFileToExtSdCard();

これが明確になり、役立つことを願っています。

于 2013-02-07T13:25:30.310 に答える