Filip_Markoski

MainActiviity

Nov 2nd, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.65 KB | None | 0 0
  1. package com.filipmarkoski.lab_intents
  2.  
  3. import android.app.Activity
  4. import android.content.Intent
  5. import android.net.Uri
  6. import android.support.v7.app.AppCompatActivity
  7. import android.os.Bundle
  8. import android.provider.ContactsContract
  9. import android.view.View
  10. import android.widget.TextView
  11. import android.provider.MediaStore
  12. import android.util.Log
  13. import android.widget.ImageView
  14. import android.widget.Toast
  15. import java.io.IOException
  16. import android.R.attr.path
  17. import android.content.ContentResolver
  18. import android.provider.OpenableColumns
  19.  
  20.  
  21. @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
  22. class MainActivity : AppCompatActivity() {
  23.  
  24.  
  25.     override fun onCreate(savedInstanceState: Bundle?) {
  26.         super.onCreate(savedInstanceState)
  27.         setContentView(R.layout.activity_main)
  28.  
  29.         // val btnExplicit:Button = findViewById(R.id.btnExplicit)
  30.  
  31.  
  32.         val extras: Bundle? = intent.extras
  33.         if (extras != null) {
  34.             val textView: TextView = findViewById(R.id.textView)
  35.             val response: String? = extras.getString("response")
  36.  
  37.             textView.text = response
  38.         }
  39.  
  40.         /* other way
  41.         textView.text = intent.getStringExtra("response")*/
  42.     }
  43.  
  44.     fun callExplicitActivity(view: View) {
  45.         val intent = Intent(this, ExplicitActivity::class.java).apply {
  46.             putExtra("value", "This is a message from MainActivity.")
  47.         }
  48.         startActivity(intent)
  49.     }
  50.  
  51.     fun callImplicitActivity(view: View) {
  52.         // Create the text message with a string
  53.         val intent = Intent()
  54.         intent.action = ImplicitActivity.IMPLICIT_ACTION
  55.  
  56.         // Verify that the intent will resolve to an activity
  57.         if (intent.resolveActivity(packageManager) != null) {
  58.             startActivity(intent)
  59.         }
  60.     }
  61.  
  62.     fun shareContent(view: View) {
  63.         // Create the text message with a string
  64.         val intent = Intent().apply {
  65.             action = Intent.ACTION_SEND
  66.             putExtra(Intent.EXTRA_TITLE, "MPiP Send Title")
  67.             putExtra(Intent.EXTRA_SUBJECT, "MPiP Send Title")
  68.             putExtra(Intent.EXTRA_TEXT, "Content send from MainActivity\nhttps://duckduckgo.com")
  69.             type = "text/plain"
  70.         }
  71.  
  72.         // Verify that the intent will resolve to an activity
  73.         if (intent.resolveActivity(packageManager) != null) {
  74.             startActivity(Intent.createChooser(intent, "Share"))
  75.         }
  76.     }
  77.  
  78.     fun selectImageAndDisplay(view: View) {
  79.         // Create the text message with a string
  80.         val intent = Intent().apply {
  81.             action = Intent.ACTION_GET_CONTENT
  82.             type = "image/*"
  83.         }
  84.  
  85.  
  86.         // Verify that the intent will resolve to an activity
  87.         if (intent.resolveActivity(packageManager) != null) {
  88.             startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_TO_APP); }
  89.     }
  90.  
  91.     fun selectImageToImageView(view: View) {
  92.         // Create the text message with a string
  93.         val intent = Intent().apply {
  94.             action = Intent.ACTION_GET_CONTENT
  95.             type = "image/*"
  96.         }
  97.  
  98.  
  99.         // Verify that the intent will resolve to an activity
  100.         if (intent.resolveActivity(packageManager) != null) {
  101.             startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); }
  102.     }
  103.  
  104.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  105.         super.onActivityResult(requestCode, resultCode, data)
  106.  
  107.         if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.data != null) {
  108.  
  109.             val uri = data.data
  110.  
  111.             try {
  112.                 val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri)
  113.                 // Log.d(TAG, String.valueOf(bitmap));
  114.  
  115.                 val imageView: ImageView = findViewById(R.id.imageView)
  116.                 imageView.setImageBitmap(bitmap)
  117.             } catch (e: IOException) {
  118.                 e.printStackTrace()
  119.             }
  120.  
  121.         }
  122.  
  123.         if (requestCode == PICK_IMAGE_REQUEST_TO_APP && resultCode == Activity.RESULT_OK && data != null && data.data != null) {
  124.  
  125.             val uri = data.data
  126.  
  127.             Toast.makeText(this, uri.toString(), Toast.LENGTH_LONG).show()
  128.             Log.i(TAG, uri.toString())
  129.  
  130.             try {
  131.  
  132.                 data.data?.let { returnUri ->
  133.                     contentResolver.query(returnUri, null, null, null, null)
  134.                 }?.use { cursor ->
  135.                     /*
  136.                      * Get the column indexes of the data in the Cursor,
  137.                      * move to the first row in the Cursor, get the data,
  138.                      * and display it.
  139.                      */
  140.                     val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
  141.                     val fileName = cursor.getString(nameIndex)
  142.                     cursor.moveToFirst()
  143.  
  144.                     Log.i(TAG, fileName)
  145.  
  146.                     val intent = Intent().apply {
  147.                         action = Intent.ACTION_VIEW
  148.                         type = "image/*"
  149.  
  150.                     }
  151.                     intent.data = Uri.parse(fileName)
  152.  
  153.                     startActivity(intent)
  154.                 }
  155.  
  156.  
  157.  
  158.  
  159.                 // intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
  160.  
  161.  
  162.             } catch (e: IOException) {
  163.                 e.printStackTrace()
  164.             }
  165.  
  166.         }
  167.     }
  168.  
  169.     companion object {
  170.         const val PICK_IMAGE_REQUEST = 1
  171.         const val PICK_IMAGE_REQUEST_TO_APP = 2
  172.  
  173.         const val TAG = "filipmarkoski.com"
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment