use of com.codename1.io.File in project CodenameOne by codenameone.
the class AndroidImplementation method createIntentForURL.
private Intent createIntentForURL(String url) {
Intent intent;
Uri uri;
try {
if (url.startsWith("intent")) {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} else {
if (url.startsWith("/") || url.startsWith("file:")) {
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to open the file")) {
return null;
}
}
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (url.startsWith("/")) {
File f = new File(url);
Uri furi = null;
try {
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
} catch (Exception ex) {
f = makeTempCacheCopy(f);
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
}
if (Build.VERSION.SDK_INT < 21) {
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
uri = furi;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
if (url.startsWith("file:")) {
File f = new File(removeFilePrefix(url));
System.out.println("File size: " + f.length());
Uri furi = null;
try {
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
} catch (Exception ex) {
f = makeTempCacheCopy(f);
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
}
if (Build.VERSION.SDK_INT < 21) {
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
uri = furi;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.parse(url);
}
}
String mimeType = getMimeType(url);
if (mimeType != null) {
intent.setDataAndType(uri, mimeType);
} else {
intent.setData(uri);
}
}
return intent;
} catch (Exception err) {
com.codename1.io.Log.e(err);
return null;
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class AndroidImplementation method openFileOutputStream.
/**
* @inheritDoc
*/
public OutputStream openFileOutputStream(String file) throws IOException {
file = removeFilePrefix(file);
OutputStream os = null;
try {
os = createFileOuputStream(file);
} catch (FileNotFoundException fne) {
// ask for the permission from the user
if (fne.getMessage().contains("Permission denied")) {
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to access the file")) {
// The user refused to give access.
return null;
} else {
// The user gave permission try again to access the path
return createFileOuputStream(file);
}
} else {
throw fne;
}
}
return os;
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class AndroidImplementation method createMedia.
/**
* @inheritDoc
*/
@Override
public Media createMedia(final String uri, boolean isVideo, final Runnable onCompletion) throws IOException {
if (getActivity() == null) {
return null;
}
if (!uri.startsWith(FileSystemStorage.getInstance().getAppHomePath())) {
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
return null;
}
}
if (uri.startsWith("file://")) {
return createMedia(removeFilePrefix(uri), isVideo, onCompletion);
}
File file = null;
if (uri.indexOf(':') < 0) {
// use a file object to play to try and workaround this issue:
// http://code.google.com/p/android/issues/detail?id=4124
file = new File(uri);
}
Media retVal;
if (isVideo) {
final AndroidImplementation.Video[] video = new AndroidImplementation.Video[1];
final boolean[] flag = new boolean[1];
final File f = file;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
VideoView v = new VideoView(getActivity());
v.setZOrderMediaOverlay(true);
if (f != null) {
v.setVideoURI(Uri.fromFile(f));
} else {
v.setVideoURI(Uri.parse(uri));
}
video[0] = new AndroidImplementation.Video(v, getActivity(), onCompletion);
flag[0] = true;
synchronized (flag) {
flag.notify();
}
}
});
while (!flag[0]) {
synchronized (flag) {
try {
flag.wait(100);
} catch (InterruptedException ex) {
}
}
}
return video[0];
} else {
MediaPlayer player;
if (file != null) {
FileInputStream is = new FileInputStream(file);
player = new MediaPlayer();
player.setDataSource(is.getFD());
player.prepare();
} else {
player = MediaPlayer.create(getActivity(), Uri.parse(uri));
}
retVal = new Audio(getActivity(), player, null, onCompletion);
}
return retVal;
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class AndroidImplementation method writeServiceProperties.
public static void writeServiceProperties(Context a) {
if (servicePropertiesDirty()) {
Map<String, String> out = getServiceProperties(a);
for (String key : servicePropertyKeys) {
String val = Display.getInstance().getProperty(key, null);
if (val != null) {
out.put(key, val);
}
if ("true".equals(Display.getInstance().getProperty(key + "#delete", null))) {
out.remove(key);
}
}
OutputStream os = null;
try {
os = a.openFileOutput("CN1$AndroidServiceProperties", 0);
if (os == null) {
System.out.println("Failed to save service properties null output stream");
return;
}
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(out.size());
for (String key : out.keySet()) {
dos.writeUTF(key);
dos.writeUTF((String) out.get(key));
}
serviceProperties = null;
} catch (FileNotFoundException ex) {
System.out.println("Service properties file not found. This is normal for the first run. On subsequent runs, the file should exist.");
} catch (IOException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (os != null)
os.close();
} catch (Throwable ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class AndroidImplementation method copy.
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[8096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
Aggregations