build.gradle (Project: MyApplication)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url "http://dl.bintray.com/lukaville/maven" } } } task clean(type: Delete) { delete rootProject.buildDir } |
build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.github.armcha:SimplePermissions:1.0.0' implementation 'com.nbsp:library:1.2' implementation 'com.koushikdutta.ion:ion:2.+' } |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="คลิกปุ่ม FAB ที่มุมล่างขวา \nเพื่อเลือกไฟล์ที่จะอัปโหลด" android:textSize="20dp" /> <ImageView android:id="@+id/img" android:layout_width="400dp" android:layout_height="250dp" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.app.Notification; import android.app.NotificationManager; import android.content.Intent; import android.widget.Toast; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.ion.Ion; import com.koushikdutta.ion.ProgressCallback; import com.luseen.simplepermission.permissions.Permission; import com.luseen.simplepermission.permissions.PermissionActivity; import com.luseen.simplepermission.permissions.SinglePermissionCallback; import com.nbsp.materialfilepicker.MaterialFilePicker; import com.nbsp.materialfilepicker.ui.FilePickerActivity; import java.io.File; public class MainActivity extends PermissionActivity { private boolean mIsUploading = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mIsUploading) { return; } requestPermission(Permission.READ_EXTERNAL_STORAGE, new SinglePermissionCallback() { @Override public void onPermissionResult(boolean granted, boolean isDeniedForever) { if (granted) { showFilePicker(); } else { Toast.makeText(getBaseContext(), "ถ้าไม่อนุญาต จะไม่สามารถเข้าถึงไฟล์ได้", Toast.LENGTH_LONG).show(); } } }); } }); } private void showFilePicker() { new MaterialFilePicker() .withActivity(MainActivity.this) .withRequestCode(555) .start(); } @Override public void onActivityResult(int rqCode, int resCode, Intent intent) { if (rqCode != 555 && resCode != RESULT_OK) { return; } String path = intent.getStringExtra(FilePickerActivity.RESULT_FILE_PATH); final Notification.Builder notifBuilder = new Notification.Builder(getBaseContext()) .setSmallIcon(R.mipmap.ic_launcher) .setContentText("กำลังอัปโหลด"); final int id = 1122; final NotificationManager notifMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mIsUploading = true; Ion.with(this) .load("http://10.0.2.2/pro-android/upload.php") .uploadProgress(new ProgressCallback() { @Override public void onProgress(long loaded, long total) { notifBuilder.setProgress((int) total, (int) loaded, false); Notification notif = notifBuilder.build(); notifMan.notify(id, notif); } }) .setMultipartFile("upload_file", new File(path)) .asString() .setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String result) { notifBuilder.setProgress(100, 100, false); notifBuilder.setContentText(result); Notification notif = notifBuilder.build(); notifMan.notify(id, notif); Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); mIsUploading = false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
บรรทัดที่ 79 : เรียกไปที่ http://10.0.2.2/pro-android/upload.php
ภาพที่ใช้