aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java
blob: 3384b54ef965fa9f601812e97d61c996966675b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package de.thedevstack.conversationsplus.utils;

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

import de.thedevstack.conversationsplus.ConversationsPlusApplication;

/**
 * Created by tzur on 30.10.2015.
 */
public final class FileHelper {

    /**
     * Get the real path from an Uri.
     * @param uri the uri to convert to the real path
     * @return the real path or <code>null</code>
     */
    public static String getRealPathFromUri(Uri uri) {
        String path = null;
        if (uri.getScheme().equals("file")) {
            return uri.getPath();
        } else if (uri.toString().startsWith("content://media/")) {
            String[] projection = {MediaStore.MediaColumns.DATA};
            Cursor metaCursor = ConversationsPlusApplication.getInstance().getContentResolver().query(uri,
                    projection, null, null, null);
            if (metaCursor != null) {
                try {
                    if (metaCursor.moveToFirst()) {
                        path = metaCursor.getString(0);
                    }
                } finally {
                    metaCursor.close();
                }
            }
        }
        return path;
    }

    private FileHelper() {
        // Utility class - do not instantiate
    }
}