如何在data / data / com.xx.xx /目录中创建新文件时设置文件权限

时间:2016-03-24 07:33:08

标签: android android-studio

文件file_address包含我的data/data/package目录

的地址

我正在设置权限

file.setReadable(true,false);
file.setExecutable(true,false);
file.setWritable(true,false);

我的文件是在-rw-------权限下创建的,但我需要-rwxrwxrwx

等权限

我该怎么办?

void downloadFile(){

    try {
        URL url = new URL(download_file_path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        //connect
        urlConnection.connect();

        //set the path where we want to save the file

        //create activity_main new file, to save the downloaded file
        File file = new File(file_address, filename_user);
        file.setReadable(true,false);
        file.setExecutable(true,false);
        file.setWritable(true,false);

        FileOutputStream fileOutput = new FileOutputStream(file);

        //Stream used for reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        //this is the total size of the file which we are downloading
        totalSize = urlConnection.getContentLength();

        runOnUiThread(new Runnable() {
            public void run() {
                pb.setMax(totalSize);
            }
        });

        //create activity_main buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
            // update the progressbar //
            runOnUiThread(new Runnable() {
                public void run() {
                    pb.setProgress(downloadedSize);
                    float per = ((float)downloadedSize/totalSize) * 100;
                    cur_val.setText("Downloaded " + downloadedSize + " / " + totalSize + " (" + (int)per + "%)" );
                }
            });
        }
        //close the output stream when complete //
        fileOutput.close();
        runOnUiThread(new Runnable() {
            public void run() {
                // pb.dismiss(); // if you want close it..
            }
        });

    } catch (final MalformedURLException e) {
        showError("Error : MalformedURLException " + e);
        e.printStackTrace();
    } catch (final IOException e) {
        showError("Error : IOException " + e);
        e.printStackTrace();
    }
    catch (final Exception e) {
        showError("Error : Please check your internet connection " + e);
    }
}

void showError(final String err){
    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(CSE_Secondyr.this, err, Toast.LENGTH_LONG).show();
        }
    });
}

0 个答案:

没有答案