2015년 10월 3일 토요일

ImgaDownloadActivity - URL의 이미지 다운로드 받기

package com.example.cadia;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageDownload extends Activity {
 String urls[] = {
   "https://lh3.googleusercontent.com/-z0uyaLaQNqw/VgJVHivw_hE/AAAAAAAAAA4/2sfr_IhbV8Q/s64-c-Ic42/September232015.jpg", 
   "https://s-media-cache-ak0.pinimg.com/736x/91/f3/08/91f30830e661cb7edffcdfad4ab3fe52.jpg"
 };
 
 String dests[] = {
   "/sdcard/Download/cadi13.jpg", 
   "/sdcard/Download/cadi14.jpg", 
 };
 
 ProgressDialog dialog = null;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_kakao);

  ImageView ImgV1 = (ImageView)findViewById(R.id.imageView1);
  ImgV1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    dialog = ProgressDialog.show(ImageDownload.this, "", "Downloading file...", true);
    new AsyncTask<Void, Void, String>() {
     @Override
     protected String doInBackground(Void... params) {
      for (int i = 0; i < urls.length; i++) {
       downloadFile(urls[i], dests[i]);
      }
      return "";
     }

     @Override
     protected void onPostExecute(String result) {
      for (int i = 0; i < urls.length; i++) {
       notifySystem(dests[i]);
      }
      hideProgressIndicator();
     }
    }.execute(null, null, null);
   }
  });
 }

 public void downloadFile(String url, String dest_file_path) {
  try {
   File dest_file = new File(dest_file_path);
   URL u = new URL(url);
   URLConnection conn = u.openConnection();
   int contentLength = conn.getContentLength();
   DataInputStream stream = new DataInputStream(u.openStream());
   byte[] buffer = new byte[contentLength];
   stream.readFully(buffer);
   stream.close();
   DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
   fos.write(buffer);
   fos.flush();
   fos.close();
  } catch(FileNotFoundException e) {
   return; 
  } catch (IOException e) {
   return; 
  }
 }

 private void notifySystem(String dest) {
  //sd카드에 파일을 쓴 후, 시스템에 알려줘야 한다. 
  MediaScannerConnection.scanFile(ImageDownload.this, 
    new String[] { dest.toString() }, 
    null,
    new MediaScannerConnection.OnScanCompletedListener() {
   public void onScanCompleted(String path, Uri uri) {
   }
  });
 }

 void hideProgressIndicator(){
  runOnUiThread(new Runnable() {
   public void run() {
    dialog.dismiss();
   }
  });
 }
}


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         >
        
        <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        </LinearLayout>

    </LinearLayout>
</ScrollView>


권한 설정
 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

댓글 없음:

댓글 쓰기