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" />

안드로이드 TTS (Text To Speech) 사용


  1. TextToSpeech tts;
  2.  
  3. tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {@Override
  4.     public void onInit(int status) {
  5.         if (status != TextToSpeech.ERROR) {
  6.             tts.setLanguage(Locale.KOREAN);
  7.         }
  8.     }
  9. });
  10.  
  11. String read = "hey";
  12. tts.speak(read, TextToSpeech.QUEUE_FLUSH, null);

2015년 9월 19일 토요일

GPS 이용해 현재 위치 얻어오기

MainActivity.java



public class MainActivity extends Activity {
    ListView listView;
    ItemListAdapter adapter;
    List<OneItem> items;
    int cnt = 0;

    public class OneItem {
        String time;
        String content;
        public OneItem(String time, String content) {
            this.time = time;
            this.content = content;
        }
    }

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

        Button button01 = (Button) findViewById(R.id.button01);
        button01.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startLocationService();
            }
        });

        items =  new ArrayList<OneItem>();

        listView = (ListView)findViewById(R.id.listView);
        adapter = new ItemListAdapter(this, R.layout.item_layout, items);
        listView.setAdapter(adapter);
    }

    class ItemListAdapter extends ArrayAdapter<OneItem> {
        private List<OneItem> items;
        private Context context;
        private int layoutResource;

        public void setContext(Context c) {
            this.context = c;
        }

        public ItemListAdapter(Context context, int layoutResource, List<OneItem> items) {
            super(context, layoutResource, items);
            this.context = context;
            this.items =  items;
            this.layoutResource = layoutResource;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(layoutResource, null);
            }

            final OneItem oneItem = items.get(position);

            if (oneItem != null) {
                TextView content = (TextView) convertView.findViewById(R.id.content);
                TextView time = (TextView) convertView.findViewById(R.id.time);

                if (content != null){
                    content.setText(oneItem.content);
                }
                if (time != null){
                    time.setText(oneItem.time);
                }
            }
            return convertView;
        }
    }

    private void startLocationService() {
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        GPSListener gpsListener = new GPSListener();
        long minTime = 1000;
        float minDistance = 0;

        try {
            //최근에 알려진 위치 얻어오기
            Location lastLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (lastLocation != null) {
                Double latitude = lastLocation.getLatitude();
                Double longitude = lastLocation.getLongitude();

                Timestamp cur = new Timestamp(System.currentTimeMillis());
                items.add(new OneItem(cur.toString(), cnt + "\n(" + latitude + "," + longitude+")"));
                cnt++;
                listView.setAdapter(adapter);
            }

            //주기적으로 GPS 정보 받도록 요청
            manager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    minTime,
                    minDistance,
                    gpsListener);

            manager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,
                    minTime,
                    minDistance,
                    gpsListener);
        }
        catch(SecurityException ex) {
            ex.printStackTrace();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    private class GPSListener implements LocationListener {
        public void onLocationChanged(Location location) {
            Double latitude = location.getLatitude();
            Double longitude = location.getLongitude();

            Timestamp cur = new Timestamp(System.currentTimeMillis());
            items.add(new OneItem(cur.toString(), cnt + "\n(" + latitude + "," + longitude+")"));
            cnt++;

            listView.setAdapter(adapter);
        }

        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }
}
activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="위치 확인 시작"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button01" />
</RelativeLayout>


item_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="content" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/content"
        android:layout_below="@+id/content"
        android:text="Time" />

</RelativeLayout>

Android Studio 사용 Tip

이제 Android Studio를 가지 않을 수 없는 거스를 수 없는 흐름이 되었다.
왜냐면 구글 공식 설명이 대부분 Android Studio만을 설명하고 있기 때문이다. ㅠㅠ
이에 몇 가지 Tip을 정리해둔다.

Auto Import

File -> Settings 에서 import로 찾아보면
Auto Import 옵션을 바꿀 수 있다.

Optimize imports on the fly 와
Add unambiguous imports on the fly 를 체크해두자.

이걸로도 자동으로 import안되는 건 하나하나 찾아서
alt + Enter를 눌러서 해야 하는 듯.

이후 발견되는 데로 계속 업데이트 예정~~

Library 추가

File -> Project Structure 에서 app 에서 Dependencies
에서 추가를 하면 다양한 라이브러리들 추가가 가능

2015년 9월 12일 토요일

SurfaceView Simple


SimpleSurfaceView.java
public class SimpleSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

 private SurfaceHolder holder;
 private Thread thread;
 
 private Bitmap image;
 private int px = 0;
 private int py = 0;
 private int vx = 1;
 private int vy = 1;
 
 public SimpleSurfaceView(Context context) {
  super(context);
  
  Resources r = getResources();
  image = BitmapFactory.decodeResource(r, R.drawable.ic_launcher);
  
  holder = getHolder();
  holder.addCallback(this);
  holder.setFixedSize(getWidth(), getHeight());
 }

 public void surfaceCreated(SurfaceHolder holder) {
  thread = new Thread(this);
  thread.start();
 }

 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  
 }
 
 public void surfaceDestroyed(SurfaceHolder holder) {
  thread = null;
 }
 
 public void stopThread() {
  thread = null;
 }
 
 @Override
 public void run() {
  Canvas canvas;
  while (thread != null) {
   canvas = holder.lockCanvas();
   canvas.drawColor(Color.WHITE);
   canvas.drawBitmap(image,  px-40,  py-40, null);
   holder.unlockCanvasAndPost(canvas);
   
   if (px < 0 || getWidth() < px ) vx = -vx;
   if (py < 0 || getHeight() < py ) vy = -vy;
   
   px += vx;
   py += vy;
  }
 }
}

SimpleSurfaceViewActivity.java
public class SimpleSurfaceViewActivity extends Activity {
 SimpleSurfaceView surfaceView;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFormat(PixelFormat.TRANSLUCENT);
  
  surfaceView = new SimpleSurfaceView(this);
  setContentView(surfaceView);
 }
 
 public void onPause() {
  super.onPause();
  surfaceView.stopThread();
  
 }
}

DB 사용하기

MyDBOpenHelper.java

public class MyDBOpenHelper extends SQLiteOpenHelper {

 private static final String DATABASE_NAME = "your_db_name";
 private static final int DATABASE_VERSION = 2;
 
 public static final String TABLE_NAME = "yout_table_name";
 
 public static final String ID = "id";
 public static final String VALUE = "value";
 
 
 public MyDBOpenHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE dbTable (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + VALUE + " TEXT);");
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.execSQL("DROP TABLE IF EXISTS dbTable");
  onCreate(db);
 }
}



DBActivity.java

public class DBActivity extends FragmentActivity implements DbAddDialogListener {
 MyDBOpenHelper myDBOpenHelper;
 SQLiteDatabase sqdb;

 ListView listView;
 DbAddEditDialog addEditDialog;
 FragmentManager fm;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.db_activity_layout);

  addEditDialog = new DbAddEditDialog();
  addEditDialog.setListener(this);
  fm = ((FragmentActivity)this).getSupportFragmentManager();

  myDBOpenHelper = new MyDBOpenHelper(this);
  sqdb = myDBOpenHelper.getWritableDatabase();

  updateList();

  Button addBtn = (Button)findViewById(R.id.addBtn);
  addBtn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    try {
     addEditDialog.setMsg("");
     addEditDialog.setDbId(null);
     addEditDialog.show(fm, null);
    } 
    catch (ClassCastException e) { }
   }
  });
 }

 private void updateList() {
  List<MyItem> list = new ArrayList<MyItem>();

  Cursor c = sqdb.query(MyDBOpenHelper.TABLE_NAME, 
    new String[] {MyDBOpenHelper.ID, MyDBOpenHelper.VALUE}, 
    null, null,null,null,null);

  while (c.moveToNext()) {
   int id = c.getInt(c.getColumnIndex(MyDBOpenHelper.ID));
   String value = c.getString(c.getColumnIndex(MyDBOpenHelper.VALUE));
   
   MyItem item = new MyItem();
   item.setId(id);
   item.setTitle(value);
   list.add(item);
  }
  c.close();
  
  CustomAdapter adapter = new CustomAdapter(this, R.layout.db_list_view_item, list);
  listView = (ListView)findViewById(R.id.listView1);
  listView.setAdapter(adapter);
 }

 class MyItem {
  private Integer id;
  private String title;
  public Integer getId() { return id; }
  public String getTitle() { return title; }
  public void setId(Integer id) { this.id = id; }
  public void setTitle(String title) { this.title = title; }
 }

 class CustomAdapter extends ArrayAdapter<MyItem> {
  private List<MyItem> items;
  private int layoutResource;
  public CustomAdapter(Context context, int layoutResource, List<MyItem> items) {
   super(context, layoutResource, items);
   this.items =  items;
   this.layoutResource = layoutResource;
  }

  @Override        
  public View getView(int position, View convertView, ViewGroup parent) {
   if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(layoutResource, null);
   }

   final MyItem myItem = items.get(position);                 
   if (myItem != null) {
    TextView title = (TextView)convertView.findViewById(R.id.textView1);                         
    if (title != null){                             
     title.setText(myItem.getTitle());           
    }
    Button editBtn = (Button)convertView.findViewById(R.id.editBtn);
    editBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      addEditDialog.setMsg(myItem.getTitle());
      addEditDialog.setDbId(myItem.getId());
      addEditDialog.show(fm, null);
     }
    });
    Button deleteBtn = (Button)convertView.findViewById(R.id.deleteBtn);
    deleteBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      sqdb.delete(MyDBOpenHelper.TABLE_NAME, MyDBOpenHelper.ID + " = ?", new String[] { String.valueOf(myItem.getId()) });
      updateList();
     }
    });
   }                 
   return convertView;         
  } 
 }

 @Override
 public void okClicked(String str, Integer id) {
  if (id == null) {
   ContentValues values = new ContentValues();  
   values.put(MyDBOpenHelper.VALUE, str);  
   sqdb.insert(MyDBOpenHelper.TABLE_NAME, null, values);  
  }
  else {
   ContentValues values = new ContentValues();  
   values.put(MyDBOpenHelper.ID, id);  
   values.put(MyDBOpenHelper.VALUE, str);  
   sqdb.update(MyDBOpenHelper.TABLE_NAME, values, MyDBOpenHelper.ID + " = ?", new String[] { String.valueOf(id) });
  }
  updateList();
 }
}



DbAddEditDialog.java

public class DbAddEditDialog extends DialogFragment {
 String msg;
 Integer dbId; 
 DbAddDialogListener listener;
 
 public Integer getDbId() { return dbId; }
 public String getMsg() { return msg; }
 public void setDbId(Integer dbId) { this.dbId = dbId; }
 public void setMsg(String msg) { this.msg = msg; }
 public void setListener(DbAddDialogListener listener) { this.listener = listener; }
 
 public interface DbAddDialogListener {
  public void okClicked(String str, Integer id);
 }
 
 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater li = LayoutInflater.from(getActivity());
  View promptsView = li.inflate(R.layout.dialog_layout, null);

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
  alertDialogBuilder.setView(promptsView);

  if (dbId != null) {
   TextView textView = (TextView)promptsView.findViewById(R.id.textView1);
   textView.setText("edit the db data");
  }
  
  final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
  userInput.setText(msg);
  alertDialogBuilder
   .setCancelable(false)
   .setPositiveButton("OK",new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     listener.okClicked(userInput.getText().toString(), dbId);
    }
   })
   .setNegativeButton("Cancel",
     new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int id) {
        dialog.cancel();
       }
     });

  AlertDialog alertDialog = alertDialogBuilder.create();

  return alertDialog;
    }
}



dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert into DB"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
        <requestFocus />
 
    </EditText>
 
</LinearLayout>



db_activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/header" >

        <Button
            android:id="@+id/addBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Add" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/addBtn"
            android:layout_alignBottom="@+id/addBtn"
            android:layout_centerHorizontal="true"
            android:text="Database" />
        
    </RelativeLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>