Get meta data using Uri of a file

This post describes how to retrieve File name, mime type, file size, and last modified from Uri of a file.

Create your layout in activity_main.xml.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="PICK FILE"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Display name"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button1" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Mime type"
        android:textSize="30sp"
        android:gravity="start"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Size"
        android:textSize="30sp"
        android:gravity="start"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView2" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Last Modified"
        android:textSize="30sp"
        android:gravity="start"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

In MainActivity.java use following codes.

package com.example.pickfile;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    TextView textview1, textview2, textview3, textview4;
    public static final int PICK_FILE = 99;

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

        Button button1 = findViewById(R.id.button1);
        textview1 = findViewById(R.id.textView1);
        textview2 = findViewById(R.id.textView2);
        textview3 = findViewById(R.id.textView3);
        textview4 = findViewById(R.id.textView4);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                startActivityForResult(intent, PICK_FILE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_FILE && resultCode == RESULT_OK){
            if (data != null){
                Uri uri = data.getData();
                displayMediaFileMetaData(uri);
            }
        }
    }

    public void displayMediaFileMetaData(Uri uri){
        try (Cursor cursor = this.getContentResolver()
                .query(uri, null, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                // DISPLAY NAME
                String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                textview1.setText(displayName);

                // MIME TYPE
                String mime = cursor.getString(cursor.getColumnIndex("mime_type"));
                textview2.setText(mime);

                // FILE SIZE
                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
                int byte_size = -1;
                if (!cursor.isNull(sizeIndex)) {
                    byte_size = cursor.getInt(sizeIndex);
                }
                if (byte_size > -1) {
                    textview3.setText(getFileSize((long) byte_size));
                }

                // LAST MODIFIED
                int lastModifiedIndex = cursor.getColumnIndex("last_modified");
                long last_mod = -1;
                if (!cursor.isNull(lastModifiedIndex)) {
                    last_mod = cursor.getLong(lastModifiedIndex);
                }
                if (last_mod > -1) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy", Locale.US);
                    textview4.setText(simpleDateFormat.format(new Date(last_mod)));
                }
            }
        }
    }

    public String getFileSize(long bytes) {
        String[] units = {"B", "KB", "MB", "GB"};
        int unit = 0;
        for (int x = 0; x < 4; x++) {
            if (bytes > Math.pow(2, 10*x)) {
                unit = x;
            }
        }
            double result = bytes/Math.pow(2, 10*unit);
            return String.format(Locale.US, "%.2f", result) + units[unit];
        }

}

Now run the app

Leave a Reply