Skip to content

Commit

Permalink
sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshub024 committed May 18, 2018
1 parent 817dbc7 commit afae76b
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 10 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':draw')
implementation 'com.android.support:design:27.1.1'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.divyanshu.androiddraw">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/divyanshu/androiddraw/DrawAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.divyanshu.androiddraw

import android.content.res.Resources
import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView

class DrawAdapter(private val filePath: ArrayList<String>) : RecyclerView.Adapter<DrawAdapter.ViewHolder>(){

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view,parent,false)
return ViewHolder(view)
}

override fun getItemCount(): Int {
return filePath.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val path = filePath[holder.adapterPosition]
holder.drawImage.setImageURI(Uri.parse(path))
}

class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
val drawImage: ImageView = itemView.findViewById(R.id.image_draw)
}

}
55 changes: 54 additions & 1 deletion app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
package com.divyanshu.androiddraw

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.support.v7.widget.GridLayoutManager
import com.divyanshu.draw.activity.DrawingActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.FileOutputStream
import java.util.*
import kotlin.collections.ArrayList

private const val REQUEST_CODE_DRAW = 101

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

startActivity(Intent(this, DrawingActivity::class.java))
recycler_view.layoutManager = GridLayoutManager(this,2)
recycler_view.adapter = DrawAdapter(getFilePath())

fab_add_draw.setOnClickListener {
val intent = Intent(this, DrawingActivity::class.java)
startActivityForResult(intent, REQUEST_CODE_DRAW)
}
}

private fun getFilePath(): ArrayList<String>{
val resultList = ArrayList<String>()
val imageDir = "${Environment.DIRECTORY_PICTURES}/Android Draw/"
val path = Environment.getExternalStoragePublicDirectory(imageDir)
val imageList = path.listFiles()
for (imagePath in imageList){
resultList.add(imagePath.absolutePath)
}
return resultList
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data != null && resultCode == Activity.RESULT_OK) {
when(requestCode){
REQUEST_CODE_DRAW -> {
val result= data.getByteArrayExtra("bitmap")
val bitmap = BitmapFactory.decodeByteArray(result, 0, result.size)
saveImage(bitmap)
}
}
}
}

private fun saveImage(bitmap: Bitmap) {
val imageDir = "${Environment.DIRECTORY_PICTURES}/Android Draw/"
val path = Environment.getExternalStoragePublicDirectory(imageDir)
val file = File(path, UUID.randomUUID().toString()+".png")
path.mkdirs()
file.createNewFile()
val outputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream)
outputStream.flush()
outputStream.close()
}
}
18 changes: 13 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add_draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_margin="16dp"
android:src="@android:drawable/ic_input_add"
android:tint="@color/color_white" />

</android.support.constraint.ConstraintLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/item_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image_draw"
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="centerCrop" />

</android.support.constraint.ConstraintLayout>
7 changes: 4 additions & 3 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#000</color>
<color name="colorPrimaryDark">#000</color>
<color name="colorAccent">#1976D2</color>
<color name="colorPrimary">#FFFFFF</color>
<color name="colorPrimaryDark">#CCCCCC</color>
<color name="colorAccent">#000</color>
<color name="titleColor">#212121</color>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down

0 comments on commit afae76b

Please sign in to comment.