My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.dahanne.android.g2android.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class UriUtils {

public final static String URL_PATTERN = "^(http|https):\\/\\/(?:\\P{M}\\p{M}*)+([\\-\\.]{1}(?:\\P{M}\\p{M}*)+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$";
public final static String IP_ADDRESS_PATTERN = "^(http|https):\\/\\/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:[0-9]{1,5})?(\\/.*)?$";

public static File createFileFromUri(InputStream openInputStream,
String mimeType) throws FileNotFoundException, IOException {

String fileExtension = null;
if (mimeType.equals("image/jpeg")) {
fileExtension = ".jpg";
} else if (mimeType.equals("image/png")) {
fileExtension = ".png";
} else if (mimeType.equals("image/gif")) {
fileExtension = ".gif";
} else {
fileExtension = ".image";
}
File imageFile = File.createTempFile("G2AndroidPhoto", fileExtension);
OutputStream out = new FileOutputStream(imageFile);
byte buf[] = new byte[1024];
int len;
while ((len = openInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
openInputStream.close();
return imageFile;
}

public static File getFileFromUri(Uri uri, Activity activity) {
String filePath = null;
String scheme = uri.getScheme();
filePath = uri.getPath();
if (filePath != null && scheme != null && scheme.equals("file")) {
return new File(filePath);
}

String[] projection = { MediaStore.Images.ImageColumns.DATA };
Cursor c = activity.managedQuery(uri, projection, null, null, null);
if (c != null && c.moveToFirst()) {
filePath = c.getString(0);
}
if (filePath != null) {
return new File(filePath);
}
return null;

}

public static String getFileNameFromUri(Uri uri, Activity activity) {
String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
String fileName = null;
Cursor c = activity.managedQuery(uri, projection, null, null, null);
if (c != null && c.moveToFirst()) {
fileName = c.getString(0);
}
return fileName;

}

public static boolean checkUrlIsValid(String url) {
Pattern p = Pattern.compile(URL_PATTERN);
Matcher m = p.matcher(url);
if (!m.matches()) {
// not an url ? maybe an ip address
p = Pattern.compile(IP_ADDRESS_PATTERN);
m = p.matcher(url);
return m.matches();
}
return true;
}

// bug #25 : for embedded gallery, should not add main.php
public static boolean isEmbeddedGallery(String url) {
if (url.contains("action=gallery")) {
return true;
}
return false;
}

public static String extractFilenameFromUri(Uri uri, Activity activity) {

String fileName = null;
String scheme = uri.getScheme();
String path = uri.getPath();
if (path != null && scheme != null && scheme.equals("file")) {
fileName = path.substring(path.lastIndexOf("/") + 1);
}

String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME /* col1 */};

Cursor c = activity.managedQuery(uri, projection, null, null, null);
if (c != null && c.moveToFirst()) {
fileName = c.getString(0);
}
return fileName;
}

}

Change log

r95 by anthony.dahanne on Aug 26, 2010   Diff
new tests to validate i18n domain names
and ip address,  issue #58  and  issue #38 
are concerned
Go to: 
Project members, sign in to write a code review

Older revisions

r90 by anthony.dahanne on Aug 25, 2010   Diff
g2android  release 1.6.0 : fixed
issues #19, #45, #46, #48, #49, #57
r85 by anthony.dahanne on Jul 19, 2010   Diff
g2android  release 1.5 :
r76 by anthony.dahanne on Mar 1, 2010   Diff
getting ready for 1.4.3; bug fixes :
#25 : embedded galleries now
available; #27 error when quickly
scrolling solved; #30 seeing g2 temp
pictures while using the gallery :
...
All revisions of this file

File info

Size: 3500 bytes, 116 lines
Powered by Google Project Hosting