How to Share Image From URL with Intent in Android?
In this article, we will see how can we share images and text with Android Intent. In this activity URL of an image to be shared will be given with extra text and we will open the Dialog where the user chooses using which he wants to share this image as shown on the screen. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/textView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "32dp" android:text = "Image Sharing Intent" android:textSize = "24sp" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> < EditText android:id = "@+id/img_url" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "28dp" android:layout_marginLeft = "20dp" android:layout_marginRight = "20dp" android:ems = "10" android:minHeight = "48dp" android:hint = "Image Url" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.497" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/textView" /> < EditText android:id = "@+id/text" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "28dp" android:layout_marginLeft = "20dp" android:layout_marginRight = "20dp" android:ems = "10" android:minHeight = "48dp" android:hint = "Extra Text" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/img_url" /> < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "44dp" android:text = "Share" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.498" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/text" /> < ProgressBar android:id = "@+id/progressBar" style = "?android:attr/progressBarStyle" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "44dp" android:visibility = "gone" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.498" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/text" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Now you will get the Layout. Let’s write the logic now.
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
package com.example.imageshare; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; public class MainActivity extends AppCompatActivity { // lateinit declaration of views EditText imgUrl, text; Button btn; ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // reference of views imgUrl = findViewById(R.id.img_url); text = findViewById(R.id.text); btn = findViewById(R.id.button); progressBar = findViewById(R.id.progressBar); // On Click listener will trigger code as soon as // some clicks on the button btn.setOnClickListener(view -> { // validation condition to check if the any text // is entered in the edit text fields or not if (TextUtils.isEmpty( imgUrl.getText().toString()) || TextUtils.isEmpty( text.getText().toString())) { Toast .makeText(MainActivity. this , "Please fill All Details" , Toast.LENGTH_SHORT) .show(); } else { progressBar.setVisibility(View.VISIBLE); btn.setVisibility(View.GONE); // Creating New thread Thread thread = new Thread(() -> URL url = null ; try { url = new URL( imgUrl.getText().toString()); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection connection = null ; try { assert url != null ; connection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); } assert connection != null ; connection.setDoInput( true ); try { connection.connect(); } catch (IOException e) { e.printStackTrace(); } InputStream input = null ; try { input = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); } Bitmap imgBitmap = BitmapFactory.decodeStream(input); Random rand = new Random(); int randNo = rand.nextInt( 100000 ); String imgBitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), imgBitmap, "IMG:" + randNo, null ); Uri imgBitmapUri = Uri.parse(imgBitmapPath); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.putExtra(Intent.EXTRA_STREAM, imgBitmapUri); shareIntent.setType( "image/png" ); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.putExtra(Intent.EXTRA_TEXT, text.getText().toString()); startActivity(Intent.createChooser(shareIntent, "Share with" )); progressBar.setVisibility(View.GONE); btn.setVisibility(View.VISIBLE); }); thread.start(); } }); } } |
Below is the Kotlin Implementation of above code. Refer the below code for MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Please keep in mind that in Kotlin, however the approach outlined below works perfectly for the simple task at hand. Moreover, according to Google Developers documentation, Coroutines are preferred over Threads.
Kotlin
import android.content.Intent import android.graphics.BitmapFactory import android.net.Uri import android.os.Bundle import android.provider.MediaStore import android.text.TextUtils import android.view.View import android.widget.Button import android.widget.EditText import android.widget.ProgressBar import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.io.IOException import java.io.InputStream import java.net.HttpURLConnection import java.net.MalformedURLException import java.net.URL import java.util.* import kotlin.concurrent.thread class MainActivity : AppCompatActivity() { // lateinit declaration of views lateinit var imgUrl: EditText lateinit var text: EditText lateinit var btn: Button lateinit var progressBar: ProgressBar override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main) // reference of views imgUrl = findViewById(R.id.img_url) text = findViewById(R.id.text) btn = findViewById(R.id.button) progressBar = findViewById(R.id.progressBar) // On Click listener will trigger code as soon as some clicks on the button btn.setOnClickListener { // validation condition to check if the any text is entered in the edit text fields or not if (TextUtils.isEmpty(imgUrl.getText().toString()) || TextUtils.isEmpty(text.getText().toString())){ Toast.makeText( this , "Please fill All Details" , Toast.LENGTH_SHORT).show() } else { // make progress bar visible progressBar.setVisibility(View.VISIBLE) btn.setVisibility(View.GONE) // Creating New thread thread { var url: URL? = null try { // get url from EditText view and converting // it to the URL url = URL(imgUrl.text.toString()) } catch (e: MalformedURLException) { // will invoke if invalid url is entered e.printStackTrace() } var connection: HttpURLConnection? = null try { assert (url != null ) connection = url!!.openConnection() as HttpURLConnection } catch (e: IOException) { e.printStackTrace() } assert (connection != null ) connection!!.doInput = true try { connection.connect() } catch (e: IOException) { e.printStackTrace() } var input: InputStream? = null try { input = connection.inputStream } catch (e: IOException) { e.printStackTrace() } // generation of image form url Input val imgBitmap = BitmapFactory.decodeStream(input) val rand = Random() val randNo = rand.nextInt( 100000 ) val imgBitmapPath = MediaStore.Images.Media.insertImage( contentResolver, imgBitmap, "IMG:$randNo" , null ) val imgBitmapUri = Uri.parse(imgBitmapPath) // share Intent val shareIntent = Intent(Intent.ACTION_SEND) shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK shareIntent.putExtra(Intent.EXTRA_STREAM, imgBitmapUri) shareIntent.type = "image/png" shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) shareIntent.putExtra(Intent.EXTRA_TEXT, text.text.toString()) // Open the chooser dialog box startActivity(Intent.createChooser(shareIntent, "Share with" )) // disappear the progress bar view progressBar.visibility = View.GONE btn.visibility = View.VISIBLE }.start() } } } } |
Now run the app in an emulator or Device you should get output like in the video.
Output: