Java Android App Development Course, Creating an Image Sharing App

Today, we will learn how to create an image sharing app in an Android environment using Java.
This course is for those who have a basic understanding of Android application development.
We will proceed step by step from project setup using Gradle, UI design, to implementing image selection and sharing features.

1. Setting Up the Development Environment

To start the project, you need to install Android Studio.
Android Studio is the official IDE for Android application development and offers various features.
Here’s how to create a project after installing Android Studio.

  1. Open Android Studio and select “New Project”.
  2. Select “Empty Activity” as the template and click the “Next” button.
  3. Set the project name to “ImageSharingApp” and configure the package name and save location as needed.
  4. Select “Java” for the Language and click “Finish” to create the project.

2. UI Configuration

Now let’s configure the user interface of the app.
We will create a simple UI that allows users to select and share images.

2.1 Modifying the Layout File

First, open the res/layout/activity_main.xml file and modify it as follows.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_select_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_below="@id/button_select_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:scaleType="centerCrop"/>

    <Button
        android:id="@+id/button_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:layout_below="@id/image_view"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"/>

</RelativeLayout>

3. Implementing Image Selection Functionality

Let’s add functionality for users to select images in the app.
To do this, we will use an image selection intent.
Open the MainActivity.java file and add the following code.

public class MainActivity extends AppCompatActivity {
    private static final int PICK_IMAGE_REQUEST = 1;

    private ImageView imageView;
    private Button buttonSelectImage, buttonShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.image_view);
        buttonSelectImage = findViewById(R.id.button_select_image);
        buttonShare = findViewById(R.id.button_share);

        buttonSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });
    }

    private void openFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Choose an image"), PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri imageUri = data.getData();
            imageView.setImageURI(imageUri);
        }
    }
}

4. Implementing Image Sharing Functionality

Now it’s time to add a feature that allows users to share the selected image with other apps.
We will add code for the sharing functionality to MainActivity.java.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ... keep the existing code

        buttonShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareImage();
            }
        });
    }

    private void shareImage() {
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
        imageView.setDrawingCacheEnabled(false);

        String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Title", "Description");
        Uri uri = Uri.parse(path);

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent, "Share the image"));
    }

5. Adding Required Permissions

To use the sharing functionality, storage permissions are required.
Add the following permission to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6. Completion and Testing

All implementations are completed.
Now we will test the app to ensure that the image selection and sharing functionalities work correctly.
Click the “Run” button in Android Studio to run the app on an emulator or a real device.

If the app is working correctly, the user can click the “Select Image” button to choose an image, and
click the “Share” button to share the selected image with other apps.

7. Conclusion

Through this tutorial, we learned how to implement image selection and sharing functionality in an Android app using Java.
This feature provides a useful experience for users and is an important element in creating practical apps.
In the future, adding additional features or improving the UI of this app will also be good learning opportunities.

Thank you! In the next tutorial, we will create an app that can be used in real situations by adding even more diverse features.