java mkdirs 目录权限_无法通过file.mkdirs()创建文…-体系课

DownloadActivity

package com.example.xp024698.handlerproject;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.support.annotation.Nullable;

import android.util.Log;

import android.view.View;

import android.widget.ProgressBar;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import static android.content.ContentValues.TAG;

public class DownloadActivity extends Activity {

public static final int DOWNLOAD_MESSAGE_CODE = 100001;

private static final int DOWNLOAD_MESSAGE_FAIL_CODE = 100002;

public static final String APP_URL ="https://boosting-site.com/wp-content/uploads/2017/05/StockSnap_9INE57RMRP.jpg";

private Handler mHanlder;

ProgressBar progressBar ;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_download);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new Thread(new Runnable() {

@Override

public void run() {

download(APP_URL);

}

}).start();

}

});

mHanlder = new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

switch(msg.what){

case DOWNLOAD_MESSAGE_CODE:

progressBar.setProgress((Integer) msg.obj);

break;

case DOWNLOAD_MESSAGE_FAIL_CODE:

break;

}

}

};

}

private void download(String appUrl) {

try {

URL url = new URL(appUrl);

URLConnection urlConnection = url.openConnection();

InputStream inputStream    = urlConnection.getInputStream();

int contenLength= urlConnection.getContentLength();

String downloadFolderName = Environment.getExternalStorageDirectory() + File.separator + "imooc" + File.separator;

File file=new File(downloadFolderName);

if(!file.exists()){

boolean isitOK = file.mkdirs();

Log.d(TAG, "downloadtekiiiiiiii: " + isitOK);

}

String fileName= downloadFolderName + "imooc.jpg";

File apkFile= new File(fileName);

if(apkFile.exists()){

apkFile.delete();

}

int downloadSize=0;

byte[] bytes = new byte[1024];

int length;

OutputStream outputStream = new FileOutputStream(fileName);

while ((length = inputStream.read(bytes)) != -1){

outputStream.write(bytes,0,length);

downloadSize+= length;

Message message = Message.obtain();

message.obj = downloadSize *100 / contenLength;

message.what= DOWNLOAD_MESSAGE_CODE;

mHanlder.sendMessage(message);

}

inputStream.close();

outputStream.close();

} catch (MalformedURLException e) {

notifyDownloadFaild();

e.printStackTrace();

} catch (IOException e) {

notifyDownloadFaild();

e.printStackTrace();

}

}

private void notifyDownloadFaild() {

Message message = Message.obtain();

message.what= DOWNLOAD_MESSAGE_FAIL_CODE;

mHanlder.sendMessage(message);

}

}Android manefest

xmlns:tools="http://schemas.android.com/tools"

package="com.example.xp024698.handlerproject"

tools:ignore="ExtraText">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

android:name=".DownloadActivity">


版权声明:本文为weixin_40004057原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。