Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />
</intent-filter>
</activity>


</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
package com.example.androidpractice2020

import androidx.appcompat.app.AppCompatActivity
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.FileNotFoundException


class MainActivity : AppCompatActivity() {
private var imageView: ImageView? = null
private val REQUEST_CODE_PICK_IMAGE = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_picture.setOnClickListener {
//Вызываем стандартную галерею для выбора изображения с помощью Intent.ACTION_PICK:
val photoPickerIntent = Intent(Intent.ACTION_PICK)
//Тип получаемых объектов - image:
photoPickerIntent.type = "image/*"

//Запускаем переход с ожиданием обратного результата в виде информации об изображении:
startActivityForResult(photoPickerIntent, REQUEST_CODE_PICK_IMAGE)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, imageReturnedIntent: Intent?) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent)
when (requestCode) {
REQUEST_CODE_PICK_IMAGE -> if (resultCode == Activity.RESULT_OK) {
try {
val imageUri = imageReturnedIntent?.data
val photoPickerIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra("img", imageUri.toString())
//в другом intent data=null
data = imageUri
type = "image/uri"
}
startActivity(photoPickerIntent)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.androidpractice2020

import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.net.Uri.parse
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.second_activity.*


class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstance: Bundle?) {
super.onCreate(savedInstance)
setContentView(R.layout.second_activity)

if (intent.getStringExtra("img") != null) {
val text = intent.getStringExtra(Intent.EXTRA_TEXT)
val uri = parse(intent.getStringExtra("img"))
message_text.text = text
val imageStream =
contentResolver.openInputStream(uri)
val selectedImage = BitmapFactory.decodeStream(imageStream)
image_first.setImageBitmap(selectedImage)
} else {
val text = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
val imageStream =
text?.let { contentResolver.openInputStream(it) }
val selectedImage = BitmapFactory.decodeStream(imageStream)
image_first.setImageBitmap(selectedImage)
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
Expand All @@ -15,4 +16,13 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Выбрать фото"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />

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

<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/image_first"
android:layout_width="113dp"
android:layout_height="108dp"
android:background="#ffffff"
app:layout_constraintBottom_toTopOf="@+id/message_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>