NSURL to File Path Monotouch - file

I am new to iphone development. I am writing my app in Mono touch.
I am trying to get a photo from the asset library which I can do successfully however I get the path as a URL "assets-library://asset/asset.JPG". I want to do a FileStream read which asks for a filepath. How can I convert NSURL to a file path?
I used asset.DefaultRepresentation.Url.AbsoluteString which gives the following filePath:
assets-library://asset/asset.JPG?id=1000000001&ext=JPG
then when it hits the FileStream(filePath, FileMode.Open, FileAccess.Read) it gives an exception stating that Could not find part of the path.
Please Help:)

The url you get from the assets library is not a file path. It is a url to a resource in the assets library, so you cannot use FileStream to read its data.
If the asset is an image, you can get its contents to an NSData object like this:
UIImage image = UIImage.FromImage(asset.DefaultRepresentation.GetImage());
NSData data = image.AsJPEG();

You can use two different static methods to load an image. FromImage and FromBundle. The first is asynchronous. The latter is synchronous (when an image is loaded the it is cashed by iOS). Both take the path to your image.
When you use these methods, property image must be set to content property. Right click on image, property -> set build action to content.

Related

Loading image from storage

I have an image that I loaded by using URLImage class. Here is the code:
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(getWidth()/2, getWidth()/2 , 0xF92D2D), true);
Image originImg = URLImage.createToStorage(placeholder, imgFileName, photo_url);
To my understanding, this image is being saved into Storage with createToStorage() method. However, next time when I am loading this form, I don't want to download the image again, I want to take it from the Storage, because its faster.
So what I did, I added check :
if (Storage.getInstance().exists(imgFileName)) {
// Take the image from the Storage
originImg = Image.createImage(Storage.getInstance().createInputStream(imgFileName));
} else {
// Load the image with URLImage class.
}
However, it seems like my file is never saved into the Storage. What can be wrong?
You don't need to do that. URLImage seamlessly loads the file from storage if the file is already there. You can open the network monitor and see if a request is made to the URL for this specific image.
I'm not sure why the Storage.getInstance().exists(imgFileName) method would fail. I assume the image name is different in the different execution or something of that sort. You can see that images are created in your .cn1 directory.

How can I overwrite an assets image in Flutter having a source image?

I'm fairly new to Dart and Flutter, and I'm having trouble to overwrite an existing assets image from a source image.
My attempt:
try {
File localFile = File('assets/images/myImage.png');
localFile.writeAsBytesSync(originFile.readAsBytesSync());
catch (e) {
log(e.toString());
}
I get:
[log] FileSystemException: Cannot open file, path = 'assets/images/myImage.png' (OS Error: No such file or directory, errno = 2)
I did define the assets folder in pubspec.yaml:
assets:
- assets/images/
Ok, so I've read somewhere that the asset file can be accessed like this:
import 'package:flutter/services.dart' show rootBundle;
final byteData = await rootBundle.load('assets/images/myImage.png');
But I don't know how to convert byteData to a File object that represents the actual file.
I think I'm missing something very basic here. Or maybe is there is a proper way to do this that has nothing to do with this approach?
Please help.
Thanks in advance!
If you want to write a file on a user device you should look here: https://flutter.dev/docs/cookbook/persistence/reading-writing-files
Shared preferences are a space in a phone where you app can write, so it's exactly what you want!
Assets are part of you app and are not meant to be modified within the app.
During a build, Flutter places assets into a special archive called
the asset bundle that apps read from at runtime. According to the flutter website
Hope this helps!

json file doesn't show up in godot

I'm making a dating sim as my first game and I want to use a JSON file to store all of my dialogue, sprites and B.G.M. kind of like a A.P.I; but the file I wrote won't appear in the godot filesystem section.
I can't get the file path without it, is there a way for it to appear or should I just give up.
Try using the File API and read the data into the variable. Like so:
var data ={}
var path = "res://data.json"
func _ready():
var jsonfile = File.new()
jsonfile.open(path, File.READ)
data = parse_json(jsonfile.get_as_text())
print(data)
pass
Also you can install JSON Editor asset from Godot marketplace - https://godotengine.org/asset-library/asset/656

How to get an Absolute URI of a file that exists in codenameone FileSystemStorage?

How to get an Absolute URI of a file that exists in codenameone FileSystemStorage?
In my project, I need to send a file that exists in CodeNameOne FileSystemStorage as an email attachment. I am using Message - getAttachments().put(...) method that forces me to use an uri (as string) of this file as a parameter.
How can I get this URI. Or better, How can I send a file in FileSistemStorage as an email attachment?
This is pretty easy, use FileSystemStorage.getAppHomePath(), and append your filename to the string it returns.
More here : https://www.codenameone.com/javadoc/index.html

make a copy of an image in blobstore

I have an image in blob store which is uploaded by users(their profile pic). I want to make a copy of the same and and re-size the copy so that it can be displayed as a thumbnail. I want to make a copy of the same instead of using the ImageService because this would be used more often compared to the profile image.
What I am doing here is this:
reader = profile_image.open() #get binary data from blob
data = reader.read()
file_name = files.blobstore.create(mime_type=profile_image.content_type)#file to write to
with files.open(file_name, 'a') as f:
f.write(data)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
image = images.Image(blob_key = blob_key)
image.resize(width=32, height=32)
entity.small_profile_pic = <MyImageModel>(caption=<caption given by user>,
picture=str(blob_key))
This is giving me error:
BadValueError: Image instance must have a complete key before it can be stored as a reference.
I think this is because the blob is not saved(put()) into the datastore, but how do i do it. Doed files.blobstore.get_blob_key(file_name) not do it ?
I would also like to ask: does the blobstore also cache the dynamically transformed images images served using get_serving_url() ...
I would use the get_serving_url method. In the doc is stated that:
The get_serving_url() method allows you to generate a stable, dedicated URL for serving web-suitable image thumbnails. You simply store a single copy of your original image in Blobstore, and then request a high-performance per-image URL. This special URL can serve that image resized and/or cropped automatically, and serving from this URL does not incur any CPU or dynamic serving load on your application (though bandwidth is still charged as usual). Images are served with low latency from a highly optimized, cookieless infrastructure.
Also the code you posted doesn't seem to follow the exampled posted in the docs. I would use something like this
img = images.Image(blob_key=original_image_key)
img.resize(width=32, height=32)
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
file_name = files.blobstore.create(mime_type='image/jpeg')#file to write to
with files.open(file_name, 'a') as f:
f.write(thumbnail)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)

Resources