Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.Activity;

import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;

import android.webkit.MimeTypeMap;

import java.io.File;

public class Main {
    public static String resolveFileName(Uri uri, Activity activity) {
        String filename = null;
        String uriString = uri.toString();

        if (uriString.startsWith("content://")) {
            Cursor cursor = null;
            try {
                cursor = activity.getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String mimeType = MimeTypeMap.getSingleton()
                            .getExtensionFromMimeType(activity.getContentResolver().getType(uri));
                    filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

                    if (mimeType != null && !filename.endsWith("." + mimeType)) {
                        filename += "." + mimeType;
                    }
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        } else if (uriString.startsWith("file://")) {
            filename = (new File(uriString)).getName();
        }
        return filename;
    }
}