我想创建允许使用网址下载图片的应用,然后在我的屏幕上显示。 不幸的是,在LogCat中显示了这个错误:
BitmapFactory:无法解码流:java.io.FileNotFoundException:sdcard / photoalbum / download_image.jpg:open failed:ENOENT(没有这样的文件或目录)
下载的进度,在屏幕上显示,效果非常快。图片有12 KB。 但是我看到这张照片没有在我的手机上下载(SD卡)。 这是因为我无法对此流进行解码?
如果有人知道如何解决/解决这个问题,我将不胜感激?
以下是代码:
ImageView imageView;
String image_url = "http://montco.happeningmag.com/wp-content/uploads/2015/04/run-150x150.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(image_url);
}
});
}
// how to create an assign task do download this image
class DownloadTask extends AsyncTask<String,Integer,String> // second type is Integer because this is from 'int progress', third is String because this is the return ("Download Complete...")
{
// progress bar to display this download
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Download in Progress...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
// how is the maximum size of this file, we need some variable:
int file_length = 0;
String path = params[0]; // we get this URL , 0(zero) index of this argument
// how image_url on this variable call "path"
try {
URL url = new URL(path);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
file_length = urlConnection.getContentLength();
// we need a folder to storage this download image
File new_folder = new File("sdcard/photoalbum");
if(!new_folder.exists())
{
new_folder.mkdir(); // we create new folder if 'photoalbum' doesnt exist in sdcard
}
// how to put some file inside this folder
File input_file = new File(new_folder,"downloaded_image.jpg");
// how to create input STREAM to read information data from url
InputStream inputStream = new BufferedInputStream(url.openStream(),8192); // we need input stream with some buffer. 8192(8 KB) (input stream
// now I want to read informations in one kb so I need byte variable
byte[] data = new byte[1024]; // it will read info to 1 KB
// before read information we need some variable
int total = 0;
int count = 0;
// we need output stream object to write a data
OutputStream outputStream = new FileOutputStream(input_file); // because outputStream is available in input_file
// we need write information to outputStream
while((count = inputStream.read())!=-1) //loop executes until the value became (-1)
{
// how to update value from a variable total
total += count;
outputStream.write(data,0,count); // data is available on the Byte variable data; offset; count
// how to display a progress bar: we need to call publish progress method and specify special value for this progress
int progress = (int) total*100/file_length;
publishProgress(progress);
}
// how to close Stream
inputStream.close();
outputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// after finished our job we need to return some result
return "Download Complete...";
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]); // this will update the progress bar
}
@Override
protected void onPostExecute(String result) {
// after finishing job, we need to hide a progress bar
progressDialog.hide();
// how to display some result
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
// how to put image into imageView
String path = "sdcard/photoalbum/download_image.jpg";
// how to set this image in imageView
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
答案 0 :(得分:0)
File new_folder = new File("sdcard/photoalbum");
if(!new_folder.exists()){
new_folder.mkdir(); // we create new folder if 'photoalbum' doesnt exist in sdcard
}
尝试修改上面的代码,如下所示:
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
File new_folder = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "photoalbum");
if(!new_folder.exists()){
new_folder.mkdirs(); // we create new folder if 'photoalbum' doesnt exist in sdcard
}
}