๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] url ์ด๋ฏธ์ง€ ํด๋”์— ์ €์žฅํ•˜๊ณ  ์ด๋ฏธ์ง€ ์—ด๊ธฐ

by _viper_ 2015. 9. 17.
๋ฐ˜์‘ํ˜•

์ด๋ฏธ์ง€ url๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋ฐ›์•„ ํด๋”์— ์ €์žฅํ•˜๊ณ  ์ €์žฅํ•œ ์ด๋ฏธ์ง€๋ฅผ ๊ฐค๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ์—ฌ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 1. ํผ๋ฏธ์…˜ ์„ค์ •  (AndroidManifest.xml)

<!-- ์ธํ„ฐ๋„ท ์ ‘์† -->
<uses-permission android:name="android.permission.INTERNET" />
    
<!-- ํŒŒ์ผ ์ƒ์„ฑ,์‚ญ์ œ ํ•˜๊ธฐ ์œ„ํ•œ -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

2. ์ž๋ฐ” ์ฝ”๋“œ ์ž‘์„ฑ (MainActivity.java)

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String imgUrl = "http://img.naver.net/static/www/u/2013/0731/nmms_224940510.gif";
        new ImageDownload().execute(imgUrl);
    }

    /**
     * url ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
     * 
     */
    private class ImageDownload extends AsyncTask<String, Void, Void> {

        /**
         * ํŒŒ์ผ๋ช…
         */
        private String fileName;

        /**
         * ์ €์žฅํ•  ํด๋”
         */
        private final String SAVE_FOLDER = "/save_folder";

        @Override
        protected Void doInBackground(String... params) {

            //๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •
            String savePath = Environment.getExternalStorageDirectory().toString() + SAVE_FOLDER;

            File dir = new File(savePath);

            //์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ƒ์„ฑ
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //ํŒŒ์ผ ์ด๋ฆ„ :๋‚ ์งœ_์‹œ๊ฐ„
            Date day = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.KOREA);
            fileName = String.valueOf(sdf.format(day));

            //์›น ์„œ๋ฒ„ ์ชฝ ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ
            String fileUrl = params[0];

            //๋‹ค์šด๋กœ๋“œ ํด๋”์— ๋™์ผํ•œ ํŒŒ์ผ๋ช…์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
            if (new File(savePath + "/" + fileName).exists() == false) {
            } else {
            }

            String localPath = savePath + "/" + fileName + ".jpg";

            try {
                URL imgUrl = new URL(fileUrl);
                //์„œ๋ฒ„์™€ ์ ‘์†ํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ๊ฐ์ฒด ์ƒ์„ฑ
                HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();
                int len = conn.getContentLength();
                byte[] tmpByte = new byte[len];
                //์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๊ตฌํ•œ๋‹ค
                InputStream is = conn.getInputStream();
                File file = new File(localPath);
                //ํŒŒ์ผ ์ €์žฅ ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
                FileOutputStream fos = new FileOutputStream(file);
                int read;
                //์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ํŒŒ์ผ๋กœ ์ €์žฅ
                for (;;) {
                    read = is.read(tmpByte);
                    if (read <= 0) {
                        break;
                    }
                    fos.write(tmpByte, 0, read); //file ์ƒ์„ฑ
                }
                is.close();
                fos.close();
                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            //์ €์žฅํ•œ ์ด๋ฏธ์ง€ ์—ด๊ธฐ
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            String targetDir = Environment.getExternalStorageDirectory().toString() + SAVE_FOLDER;
            File file = new File(targetDir + "/" + fileName + ".jpg");
            //type ์ง€์ • (์ด๋ฏธ์ง€)
            i.setDataAndType(Uri.fromFile(file), "image/*");
            getApplicationContext().startActivity(i);
            //์ด๋ฏธ์ง€ ์Šค์บ”ํ•ด์„œ ๊ฐค๋Ÿฌ๋ฆฌ ์—…๋ฐ์ดํŠธ
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        }
    }

}

 ์•ฑ์„ ์‹คํ–‰ํ•˜๋ฉด ํด๋”์— ์ž๋™์œผ๋กœ ์ €์žฅ๋˜๊ณ  ์ €์žฅํ•œ ์ด๋ฏธ์ง€๋ฅผ ๊ฐค๋Ÿฌ๋ฆฌ์—์„œ ๋ฐ”๋กœ ์—ด์–ด์ค๋‹ˆ๋‹ค.

 

[๊ฒฐ๊ณผํ™”๋ฉด]