Wednesday, 14 March 2018

Membuat Browse File / File chooser / File Explorer -Android

Gambar 1. Browse File



Mumpung sedang di Manokwari ada koneksi kencang nih..

Sebenarnya Android menyediakan fasilitas library browse file/ access framework tinggal pakai yaitu menggunakan intent

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

Namun output dari yang diberikan tidak bisa memenuhi semua kebutuhan, contohnya pada saat file yang kita pilih ingin diconvert ke object File. Alasanya tidak bisa diconvert yaitu agar file tetap secure.  Hasil fungsi getPath yang kita dapatkan dari intent bukan real path sehingga ketika path tersebut dimasukan ke dalam object File tidak akan terbaca/not exist.
Tutorial penggunaan library browse file/ access framework:
https://developer.android.com/guide/topics/providers/document-provider.html


Jika kita ingin browse file dan kemudian file tersebut diolah lagi menggunakan Object File gunakan custom browse file. Source code costom browse file: (penjelasan menyusul)

activity_fileexplorer.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Get File Name From SDCard"
        android:textSize="18dp"
        android:gravity="center"
        android:layout_marginTop="10dp"
        />
    <RelativeLayout android:id="@+id/relativeLayout1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <EditText
            android:layout_alignParentLeft="true"
            android:hint="EditText"
            android:id="@+id/editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="15dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:layout_toLeftOf="@+id/skipButton" >
        </EditText>

        <Button android:text="Browser"
            android:id="@+id/skipButton"
            android:textSize="18dp"
            android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:onClick="getfile" >
        </Button>
    </RelativeLayout>
</LinearLayout>

file_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent">
    <ImageView
        android:id="@+id/fd_Icon1"
        android:layout_width="50dip"
        android:layout_height="50dip" >
    </ImageView>

    <TextView android:text="@+id/TextView01"
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:layout_marginTop="5dip"
        android:layout_marginLeft="5dip">
    </TextView>
    <TextView android:text="@+id/TextView02"
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:layout_below="@+id/TextView01"
        android:layout_marginLeft="10dip">

    </TextView>
    <TextView android:text="@+id/TextViewDate"
        android:id="@+id/TextViewDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="5dip">
    </TextView>
</RelativeLayout>

MainActivity.java:
package com.example.mywindows.fileeksplorer2;

         import android.os.Bundle;
         import android.app.Activity;
         import android.content.Intent;
         import android.view.View;
         import android.widget.EditText;

public class MainActivity extends Activity {

    private static final int REQUEST_PATH = 1;
    String curFileName;
    EditText edittext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fileexplorer);
        edittext = (EditText)findViewById(R.id.editText);
    }

    public void getfile(View view){
        Intent intent1 = new Intent(this, FileChooser.class);
        startActivityForResult(intent1,REQUEST_PATH);
    }
    // Listen for results.
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        // See which child activity is calling us back.
        if (requestCode == REQUEST_PATH){
            if (resultCode == RESULT_OK) {
                curFileName = data.getStringExtra("GetFileName");
                edittext.setText(curFileName);
            }
        }
    }
}

Item.java
 class Item implements Comparable<Item>{
    private String name;
    private String data;
    private String date;
    private String path;
    private String image;

    public Item(String n,String d, String dt, String p, String img)
    {
        name = n;
        data = d;
        date = dt;
        path = p;
        image = img;
    }
    public String getName()
    {
        return name;
    }
    public String getData()
    {
        return data;
    }
    public String getDate()
    {
        return date;
    }
    public String getPath()
    {
        return path;
    }
    public String getImage() {
        return image;
    }
    public int compareTo(Item o) {
        if(this.name != null)
            return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
        else
            throw new IllegalArgumentException();
    }
}

Filechoose.java:
(File yang dapat dipilih hanya yang berekstensi .xls karna ada kode
if(ff.getName().length()-4==ff.getName().lastIndexOf(".xls"))
kode ini dapat dihilangkan jika tidak ingin pengecualian ekstensi)
package com.example.mywindows.fileeksplorer2;

/**
 * Created by MYWINDOWS on 3/14/2018.
 */

import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.text.DateFormat;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class FileChooser extends ListActivity {

    private File currentDir;
    private FileArrayAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentDir = new File("/sdcard/");
        fill(currentDir);
    }
    private void fill(File f)
    {
        File[]dirs = f.listFiles();
        this.setTitle("Current Dir: "+f.getName());
        List<Item>dir = new ArrayList<Item>();
        List<Item>fls = new ArrayList<Item>();
        try{
            for(File ff: dirs)
            {
                Date lastModDate = new Date(ff.lastModified());
                DateFormat formater = DateFormat.getDateTimeInstance();
                String date_modify = formater.format(lastModDate);
                if(ff.isDirectory()){


                    File[] fbuf = ff.listFiles();
                    int buf = 0;
                    if(fbuf != null){
                        buf = fbuf.length;
                    }
                    else buf = 0;
                    String num_item = String.valueOf(buf);
                    if(buf == 0) num_item = num_item + " item";
                    else num_item = num_item + " items";

                    //String formated = lastModDate.toString();
                    dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
                }
                else
                {
                 //  if(==0)
if(ff.getName().length()-4==ff.getName().lastIndexOf(".xls"))
                    fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
                }
            }
        }catch(Exception e)
        {

        }
        Collections.sort(dir);
        Collections.sort(fls);
        dir.addAll(fls);
        if(!f.getName().equalsIgnoreCase("sdcard"))
            dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
        adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
        this.setListAdapter(adapter);
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Item o = adapter.getItem(position);
        if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
            currentDir = new File(o.getPath());
            fill(currentDir);
        }
        else
        {
            onFileClick(o);
        }
    }
    private void onFileClick(Item o)
    {
        //Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("GetPath",currentDir.toString());
        intent.putExtra("GetFileName",o.getName());
        setResult(RESULT_OK, intent);
        finish();
    }
}

FileArrayAdapter.java:
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileArrayAdapter extends ArrayAdapter<Item>{

    private Context c;
    private int id;
    private List<Item>items;

    public FileArrayAdapter(Context context, int textViewResourceId,
                            List<Item> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }
    public Item getItem(int i)
    {
        return items.get(i);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(id, null);
        }

               /* create a new view of my layout and inflate it in the row */
        //convertView = ( RelativeLayout ) inflater.inflate( resource, null );

        final Item o = items.get(position);
        if (o != null) {
            TextView t1 = (TextView) v.findViewById(R.id.TextView01);
            TextView t2 = (TextView) v.findViewById(R.id.TextView02);
            TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
                       /* Take the ImageView from layout and set the city's image */
            ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
            String uri = "drawable/" + o.getImage();
            int C = c.getResources().getIdentifier(uri, "drawable", c.getPackageName());
            Drawable image;
Log.e("1231",C+"");
            if(C!=0){
            image = c.getResources().getDrawable(C);
       //  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
         //    image = c.getDrawable(C);
           // } else{
            //}
          imageCity.setImageDrawable(image);}

            if(t1!=null)
                t1.setText(o.getName());
            if(t2!=null)
                t2.setText(o.getData());
            if(t3!=null)
                t3.setText(o.getDate());
        }
        return v;
    }



Copy file_icon.png dan directory_icon.png ke drawable. PNG tersebut yang akan memberikan gambar icon pada tiap file.


Selamat bereksperimen
Sekian dan terimakasih ...

Wednesday, 21 June 2017

Cara Mengatur / Membuat Password phpmyadmin pada xampp Server



Biasanya sebagai programmer membuat aplikasi web menggunakan tools local server seperti xampp. Untuk melindungi kerahasiaan dari data yang tersimpan pada server tersebut kita perlu mengatur username dan password. Nah berikut cara memberikan / mengatur password dan username :

1. Jalankan Apache dan MySQL

Gambar 1. Menjalankan Apache dan MySQL


2. Buka config.inc.php pada C:\xampp\phpMyAdmin\config.inc.php 

Gambar 2. config.inc

3. Ubah
$cfg['Servers'][$i]['auth_type'] = 'config';  menjadi $cfg['Servers'][$i]['auth_type'] = 'cookie'; . Jika perlu ubah juga usernamenya
Gambar 3. Ubah authtype
4. Reload browser / localhost. Login sesuai dengan username tadi
Gambar 4. Reload browser
5. Ubah / atur password

Gambar 5. Pilih change password
6. Masukan password yang diinginkan
Gambar 6. Mengatur password


7. Berhasil password sudah terpasang. silah logout untuk mencoba login,
Gambar 7. Pilih Logout untuk mencoba password


Sekian dan trima kasih
Selamat bereksperimen!!!





Saturday, 10 June 2017

Cara Menggunakan Library Fullcalendar 1 -javascript

Pada posting kali ini hanya akan memaparkan bagaimana menampilkan Fullcalendar di localhost. Fullcalendar dapat berguna untuk menampilkan kalender kegiatan. Sehingga jadwal kegiatan dapat dimonitoring dengan visualisasi yang menarik. Fullcalendar saya gunakan untuk memenejemen pekerjaan kantor yang mana banyak pegawai dan pekerjaan. Sehingga tidak ada tumpang tindih dan pekerjaan dapat lebih dibagi dengan merata.

link fullcalendar:
https://fullcalendar.io/

1. fullcalendar dapat didownload:
https://github.com/fullcalendar/fullcalendar/releases

2. ektrak ke direktori (saya membuat new folder kalender pada localhost/htdocs)

3. kemudian akses demonya dengan link
http://localhost/kalender/demos/default.html


hasil:
Mempelajari fullcalendar dapat menggunakan dokumentasi  situs resminya dan dapat melihat source code demos nya

hasil implementasi sementara yang telah saya buat:



Nantikan tutorial selanjutnya
selamat bereksperimen dan terimakasih..