Android中的权限被拒绝如何解决?

时间:2013-01-25 11:48:15

标签: android permissions download

我尝试此代码,但我总是显示错误权限被拒绝。我尝试下面的代码。

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hope.project"
    android:versionCode="1"
    android:versionName="1.0">
     <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
      <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">

         <activity android:name="com.hope.project.MainActivity" android:label="@string/app_name"
                android:theme="@android:style/Theme.Black.NoTitleBar"
                android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我显示错误:

 01-25 17:24:17.853: DEBUG/szipinf(12612): Initializing inflate state
01-25 17:24:17.853: DEBUG/szipinf(12612): Initializing zlib to inflate
01-25 17:24:19.201: DEBUG/dalvikvm(338): GC_EXPLICIT freed 17K, 5% free 6276K/6599K, paused 3ms+115ms
01-25 17:24:25.424: DEBUG/dalvikvm(206): GC_EXPLICIT freed 153K, 6% free 6730K/7111K, paused 4ms+94ms
01-25 17:25:11.912: DEBUG/SntpClient(71): request time failed: java.net.SocketException: Address family not supported by protocol
01-25 17:25:24.961: VERBOSE/log(12612): onPageFinished: file:///android_asset/www/index.html#goToNode/2
01-25 17:25:27.276: VERBOSE/log(12612):  on ovverRide about:blank
01-25 17:25:44.464: VERBOSE/log(12612): onPageFinished: file:///android_asset/www/index.html#goToNode/328
01-25 17:26:25.692: VERBOSE/log(12612): onPageFinished: file:///android_asset/www/index.html#goToNode/10
01-25 17:26:25.901: DEBUG/dalvikvm(12612): GC_CONCURRENT freed 619K, 10% free 7242K/8007K, paused 4ms+3ms
01-25 17:26:33.711: VERBOSE/log(12612): login main url http://imprintingdesign.com/hiren_testing/TestHopeNew/testHope/pdf/TheMountain.mp4
01-25 17:26:33.781: VERBOSE/log(12612): login main url[0] http://imprintingdesign.com/hiren_testing/TestHopeNew/testHope/pdf/TheMountain.mp4
01-25 17:26:35.631: ERROR/Error:(12612): /mnt/sdcard/Downloads/TheMountain.mp4 (Permission denied)

我下载的课程如下:

public class MainActivity extends Activity {

    WebView myWebView;
    TextView mDisplay;
    AsyncTask<Void, Void, Void> mRegisterTask;

    String Message;
    String deviceId;
    String regId;
    IntentFilter gcmFilter;
    SharedPreferences sharedPref;
    private final String PATH = "/sdcard/download/";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView)findViewById(R.id.webView1);


        final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(
                this);
        myWebView.addJavascriptInterface(myJavaScriptInterface,
                "AndroidFunction");

        WebSettings settings = myWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadWithOverviewMode(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


         myWebView.setWebViewClient(new WebViewClient() {  
               public boolean shouldOverrideUrlLoading(WebView view, String url)  {                  

                            //handle stuff here 
                            //e.g. view.loadUrl(url);   
                   Log.v("log"," on ovverRide " + url);
                            return true;    
                        }

                        public void onPageFinished(WebView view, String url) {
                            //dismiss the indeterminate progress dialog
                            Log.v("log", "onPageFinished: " + url);
                            myWebView.setEnabled(false);

                        } 
                    });

        myWebView.loadUrl("file:///android_asset/www/index.html");


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }
        public void DownloadUrl(String url) {
            Log.v("log","login main url " + url);

            String file_url = url;
            new DownloadFileFromURL().execute(file_url);
            //new DownloadFromUrl(v,g);

            /*String url_new = "http://"+url;
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url_new));
            startActivity(i);*/

        }
    }


    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
             Log.v("log","login main url[0] " + f_url[0]);
            try {
                URL url = new URL(f_url[0]);
                String name = f_url[0].substring(f_url[0].lastIndexOf("/") + 1);
                URLConnection conection = url.openConnection();
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 1024 * 5);

                // Output stream to write file
                //OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
                OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Downloads/"
                + name);
                byte data[] = new byte[5 * 1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress(""+(int)((total*100)/lenghtOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) 
        {
            // setting progress percentage
            //pDialog.setProgress(Integer.parseInt(progress[0]));
       }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            //dismissDialog(progress_bar_type);

            // Displaying downloaded image into image view
            // Reading image path from sdcard
            //String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
            // setting downloaded into image view
            //my_image.setImageDrawable(Drawable.createFromPath(imagePath));
        }

    }

1 个答案:

答案 0 :(得分:0)

在AndroidManifest.xml中,选择编辑器底部的“Permissions”选项卡(Manifest - Application - Permissions - Instrumentation - AndroidManifest.xml),然后选择“Add ...”,“使用权限”并选择所需的右侧下拉菜单的权限,或者只需复制必要的粘贴(例如您需要的“android.permission.INTERNET”权限)。

有关详情,请点击此链接:http://developer.android.com/reference/android/Manifest.permission.html