-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817dbc7
commit afae76b
Showing
10 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/divyanshu/androiddraw/DrawAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters