├── .gitignore ├── .idea ├── .name ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── dictionaries │ └── belal.xml ├── gradle.xml ├── misc.xml ├── render.experimental.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── net │ │ └── simplifiedcoding │ │ └── imageuploader │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── net │ │ │ └── simplifiedcoding │ │ │ └── imageuploader │ │ │ ├── MainActivity.kt │ │ │ ├── MyAPI.kt │ │ │ ├── UploadRequestBody.kt │ │ │ ├── UploadResponse.kt │ │ │ └── Utils.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ ├── ic_picture.xml │ │ └── ic_upload.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── net │ └── simplifiedcoding │ └── imageuploader │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Image Uploader -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | xmlns:android 17 | 18 | ^$ 19 | 20 | 21 | 22 |
23 |
24 | 25 | 26 | 27 | xmlns:.* 28 | 29 | ^$ 30 | 31 | 32 | BY_NAME 33 | 34 |
35 |
36 | 37 | 38 | 39 | .*:id 40 | 41 | http://schemas.android.com/apk/res/android 42 | 43 | 44 | 45 |
46 |
47 | 48 | 49 | 50 | .*:name 51 | 52 | http://schemas.android.com/apk/res/android 53 | 54 | 55 | 56 |
57 |
58 | 59 | 60 | 61 | name 62 | 63 | ^$ 64 | 65 | 66 | 67 |
68 |
69 | 70 | 71 | 72 | style 73 | 74 | ^$ 75 | 76 | 77 | 78 |
79 |
80 | 81 | 82 | 83 | .* 84 | 85 | ^$ 86 | 87 | 88 | BY_NAME 89 | 90 |
91 |
92 | 93 | 94 | 95 | .* 96 | 97 | http://schemas.android.com/apk/res/android 98 | 99 | 100 | ANDROID_ATTRIBUTE_ORDER 101 | 102 |
103 |
104 | 105 | 106 | 107 | .* 108 | 109 | .* 110 | 111 | 112 | BY_NAME 113 | 114 |
115 |
116 |
117 |
118 | 119 | 121 |
122 |
-------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/dictionaries/belal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | snackbar 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/render.experimental.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 29 9 | buildToolsVersion "29.0.2" 10 | defaultConfig { 11 | applicationId "net.simplifiedcoding.imageuploader" 12 | minSdkVersion 21 13 | targetSdkVersion 29 14 | versionCode 1 15 | versionName "1.0" 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 29 | implementation 'androidx.appcompat:appcompat:1.1.0' 30 | implementation 'androidx.core:core-ktx:1.2.0' 31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 35 | 36 | implementation 'com.squareup.retrofit2:retrofit:2.6.1' 37 | implementation 'com.squareup.retrofit2:converter-gson:2.6.1' 38 | implementation 'com.google.android.material:material:1.1.0' 39 | 40 | } 41 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/simplifiedcoding/imageuploader/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("net.simplifiedcoding.imageuploader", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/imageuploader/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | import android.app.Activity 4 | import android.content.ContentResolver 5 | import android.content.Intent 6 | import android.net.Uri 7 | import android.os.Bundle 8 | import android.provider.OpenableColumns 9 | import androidx.appcompat.app.AppCompatActivity 10 | import kotlinx.android.synthetic.main.activity_main.* 11 | import okhttp3.MediaType 12 | import okhttp3.MultipartBody 13 | import okhttp3.RequestBody 14 | import retrofit2.Call 15 | import retrofit2.Callback 16 | import retrofit2.Response 17 | import java.io.File 18 | import java.io.FileInputStream 19 | import java.io.FileOutputStream 20 | 21 | 22 | class MainActivity : AppCompatActivity(), UploadRequestBody.UploadCallback { 23 | 24 | private var selectedImageUri: Uri? = null 25 | 26 | override fun onCreate(savedInstanceState: Bundle?) { 27 | super.onCreate(savedInstanceState) 28 | setContentView(R.layout.activity_main) 29 | 30 | image_view.setOnClickListener { 31 | openImageChooser() 32 | } 33 | 34 | button_upload.setOnClickListener { 35 | uploadImage() 36 | } 37 | } 38 | 39 | private fun openImageChooser() { 40 | Intent(Intent.ACTION_PICK).also { 41 | it.type = "image/*" 42 | val mimeTypes = arrayOf("image/jpeg", "image/png") 43 | it.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes) 44 | startActivityForResult(it, REQUEST_CODE_PICK_IMAGE) 45 | } 46 | } 47 | 48 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 49 | super.onActivityResult(requestCode, resultCode, data) 50 | if (resultCode == Activity.RESULT_OK) { 51 | when (requestCode) { 52 | REQUEST_CODE_PICK_IMAGE -> { 53 | selectedImageUri = data?.data 54 | image_view.setImageURI(selectedImageUri) 55 | } 56 | } 57 | } 58 | } 59 | 60 | private fun uploadImage() { 61 | if (selectedImageUri == null) { 62 | layout_root.snackbar("Select an Image First") 63 | return 64 | } 65 | 66 | val parcelFileDescriptor = 67 | contentResolver.openFileDescriptor(selectedImageUri!!, "r", null) ?: return 68 | 69 | val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor) 70 | val file = File(cacheDir, contentResolver.getFileName(selectedImageUri!!)) 71 | val outputStream = FileOutputStream(file) 72 | inputStream.copyTo(outputStream) 73 | 74 | progress_bar.progress = 0 75 | val body = UploadRequestBody(file, "image", this) 76 | MyAPI().uploadImage( 77 | MultipartBody.Part.createFormData( 78 | "image", 79 | file.name, 80 | body 81 | ), 82 | RequestBody.create(MediaType.parse("multipart/form-data"), "json") 83 | ).enqueue(object : Callback { 84 | override fun onFailure(call: Call, t: Throwable) { 85 | layout_root.snackbar(t.message!!) 86 | progress_bar.progress = 0 87 | } 88 | 89 | override fun onResponse( 90 | call: Call, 91 | response: Response 92 | ) { 93 | response.body()?.let { 94 | layout_root.snackbar(it.message) 95 | progress_bar.progress = 100 96 | } 97 | } 98 | }) 99 | 100 | } 101 | 102 | override fun onProgressUpdate(percentage: Int) { 103 | progress_bar.progress = percentage 104 | } 105 | 106 | companion object { 107 | const val REQUEST_CODE_PICK_IMAGE = 101 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/imageuploader/MyAPI.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | import okhttp3.MultipartBody 4 | import okhttp3.RequestBody 5 | import retrofit2.Call 6 | import retrofit2.Retrofit 7 | import retrofit2.converter.gson.GsonConverterFactory 8 | import retrofit2.http.Multipart 9 | import retrofit2.http.POST 10 | import retrofit2.http.Part 11 | 12 | interface MyAPI { 13 | 14 | @Multipart 15 | @POST("Api.php?apicall=upload") 16 | fun uploadImage( 17 | @Part image: MultipartBody.Part, 18 | @Part("desc") desc: RequestBody 19 | ): Call 20 | 21 | companion object { 22 | operator fun invoke(): MyAPI { 23 | return Retrofit.Builder() 24 | .baseUrl("http://10.10.10.118/ImageUploader/") 25 | .addConverterFactory(GsonConverterFactory.create()) 26 | .build() 27 | .create(MyAPI::class.java) 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/imageuploader/UploadRequestBody.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | import android.os.Handler 4 | import android.os.Looper 5 | import okhttp3.MediaType 6 | import okhttp3.RequestBody 7 | import okio.BufferedSink 8 | import java.io.File 9 | import java.io.FileInputStream 10 | 11 | class UploadRequestBody( 12 | private val file: File, 13 | private val contentType: String, 14 | private val callback: UploadCallback 15 | ) : RequestBody() { 16 | 17 | override fun contentType() = MediaType.parse("$contentType/*") 18 | 19 | override fun contentLength() = file.length() 20 | 21 | override fun writeTo(sink: BufferedSink) { 22 | val length = file.length() 23 | val buffer = ByteArray(DEFAULT_BUFFER_SIZE) 24 | val fileInputStream = FileInputStream(file) 25 | var uploaded = 0L 26 | fileInputStream.use { inputStream -> 27 | var read: Int 28 | val handler = Handler(Looper.getMainLooper()) 29 | while (inputStream.read(buffer).also { read = it } != -1) { 30 | handler.post(ProgressUpdater(uploaded, length)) 31 | uploaded += read 32 | sink.write(buffer, 0, read) 33 | } 34 | } 35 | } 36 | 37 | interface UploadCallback { 38 | fun onProgressUpdate(percentage: Int) 39 | } 40 | 41 | inner class ProgressUpdater( 42 | private val uploaded: Long, 43 | private val total: Long 44 | ) : Runnable { 45 | override fun run() { 46 | callback.onProgressUpdate((100 * uploaded / total).toInt()) 47 | } 48 | } 49 | 50 | companion object { 51 | private const val DEFAULT_BUFFER_SIZE = 2048 52 | } 53 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/imageuploader/UploadResponse.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | data class UploadResponse( 4 | val error: Boolean, 5 | val message: String, 6 | val image: String 7 | ) -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/imageuploader/Utils.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.imageuploader 2 | 3 | import android.content.ContentResolver 4 | import android.net.Uri 5 | import android.provider.OpenableColumns 6 | import android.view.View 7 | import com.google.android.material.snackbar.Snackbar 8 | 9 | fun View.snackbar(message: String) { 10 | Snackbar.make( 11 | this, 12 | message, 13 | Snackbar.LENGTH_LONG 14 | ).also { snackbar -> 15 | snackbar.setAction("Ok") { 16 | snackbar.dismiss() 17 | } 18 | }.show() 19 | } 20 | 21 | fun ContentResolver.getFileName(fileUri: Uri): String { 22 | var name = "" 23 | val returnCursor = this.query(fileUri, null, null, null, null) 24 | if (returnCursor != null) { 25 | val nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) 26 | returnCursor.moveToFirst() 27 | name = returnCursor.getString(nameIndex) 28 | returnCursor.close() 29 | } 30 | return name 31 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_picture.xml: -------------------------------------------------------------------------------- 1 | 7 | 9 | 10 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 32 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 42 | 43 | 45 | 46 | 48 | 49 | 55 | 56 | 57 | 58 | 59 | 60 | 62 | 63 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 78 | 79 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 95 | 96 | 98 | 99 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_upload.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 20 | 21 | 27 | 28 | 36 | 37 | 44 | 45 | 46 | 47 |