Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.filipmarkoski.lab_intents
- import android.app.Activity
- import android.content.Intent
- import android.net.Uri
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.provider.ContactsContract
- import android.view.View
- import android.widget.TextView
- import android.provider.MediaStore
- import android.util.Log
- import android.widget.ImageView
- import android.widget.Toast
- import java.io.IOException
- import android.R.attr.path
- import android.content.ContentResolver
- import android.provider.OpenableColumns
- @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- // val btnExplicit:Button = findViewById(R.id.btnExplicit)
- val extras: Bundle? = intent.extras
- if (extras != null) {
- val textView: TextView = findViewById(R.id.textView)
- val response: String? = extras.getString("response")
- textView.text = response
- }
- /* other way
- textView.text = intent.getStringExtra("response")*/
- }
- fun callExplicitActivity(view: View) {
- val intent = Intent(this, ExplicitActivity::class.java).apply {
- putExtra("value", "This is a message from MainActivity.")
- }
- startActivity(intent)
- }
- fun callImplicitActivity(view: View) {
- // Create the text message with a string
- val intent = Intent()
- intent.action = ImplicitActivity.IMPLICIT_ACTION
- // Verify that the intent will resolve to an activity
- if (intent.resolveActivity(packageManager) != null) {
- startActivity(intent)
- }
- }
- fun shareContent(view: View) {
- // Create the text message with a string
- val intent = Intent().apply {
- action = Intent.ACTION_SEND
- putExtra(Intent.EXTRA_TITLE, "MPiP Send Title")
- putExtra(Intent.EXTRA_SUBJECT, "MPiP Send Title")
- putExtra(Intent.EXTRA_TEXT, "Content send from MainActivity\nhttps://duckduckgo.com")
- type = "text/plain"
- }
- // Verify that the intent will resolve to an activity
- if (intent.resolveActivity(packageManager) != null) {
- startActivity(Intent.createChooser(intent, "Share"))
- }
- }
- fun selectImageAndDisplay(view: View) {
- // Create the text message with a string
- val intent = Intent().apply {
- action = Intent.ACTION_GET_CONTENT
- type = "image/*"
- }
- // Verify that the intent will resolve to an activity
- if (intent.resolveActivity(packageManager) != null) {
- startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_TO_APP); }
- }
- fun selectImageToImageView(view: View) {
- // Create the text message with a string
- val intent = Intent().apply {
- action = Intent.ACTION_GET_CONTENT
- type = "image/*"
- }
- // Verify that the intent will resolve to an activity
- if (intent.resolveActivity(packageManager) != null) {
- startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.data != null) {
- val uri = data.data
- try {
- val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri)
- // Log.d(TAG, String.valueOf(bitmap));
- val imageView: ImageView = findViewById(R.id.imageView)
- imageView.setImageBitmap(bitmap)
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- if (requestCode == PICK_IMAGE_REQUEST_TO_APP && resultCode == Activity.RESULT_OK && data != null && data.data != null) {
- val uri = data.data
- Toast.makeText(this, uri.toString(), Toast.LENGTH_LONG).show()
- Log.i(TAG, uri.toString())
- try {
- data.data?.let { returnUri ->
- contentResolver.query(returnUri, null, null, null, null)
- }?.use { cursor ->
- /*
- * Get the column indexes of the data in the Cursor,
- * move to the first row in the Cursor, get the data,
- * and display it.
- */
- val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
- val fileName = cursor.getString(nameIndex)
- cursor.moveToFirst()
- Log.i(TAG, fileName)
- val intent = Intent().apply {
- action = Intent.ACTION_VIEW
- type = "image/*"
- }
- intent.data = Uri.parse(fileName)
- startActivity(intent)
- }
- // intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- }
- companion object {
- const val PICK_IMAGE_REQUEST = 1
- const val PICK_IMAGE_REQUEST_TO_APP = 2
- const val TAG = "filipmarkoski.com"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment