How to upload image using App42 - android

I have a WebApi project and a Android client, and I need to upload some images from the WebApi using App42, the problem is, in this tutorial, it must have a StringPath, but I dont have a stringPath, because the image is gonna be send by the Android Client for the Api and then Save those images. Do I have to send the image from Android as Base64 and then create a tempFileObject as Bitmap and then put the stringPath?
String name = "MyPic";
String userName = "Nick";
String filePath = " file path from gallery /sd card";
String fileType = "IMAGE";
String description = "This is my holiday pic";
Upload upload = uploadService.UploadFileForUser(name,userName,filePath,fileType,description);
// File will get uploaded in App42 cloud with above snippet.
IList<Upload.File> fileList = upload.GetFileList(); // This will have only single file uploaded above
for (int i = 0; i < fileList.Count; i++)
{
Console.WriteLine("fileUrl is " + fileList[i].GetUrl());
//This will return uploaded file URL
}

If I understood the query then you don't have file saved in the device local file storage. If Yes, then you can use another API which will ask you the InputSteam of the image as parameter.
I hope this helps.

Related

Image url start with https not downloading android

I'm downloading image using url like below,
final String url = article.image.url;
String parentFolder = ""+article.category_id;
final String fileName = ""+article.image.id;
FileManager fileManager = BDevice.getFileManager(mContext);
final File file = BDevice.getFileManager(mContext).getFile(""+article.category_id, parentFolder, fileName);
Uri uri = null;
if (fileManager.isFileExists(file.getAbsolutePath())) {
uri = Uri.fromFile(file);
} else {
if (url != null) {
uri = Uri.parse(url);
}
}
holder.image.setImageURI(uri);
But this is working for this url "https://citylanedev.blob.core.windows.net/citylane/2016/10/Pharmacie_Icon.png"
and not working for this url "https://dev.citylaneapp.com/wp-content/uploads/2016/09/france-mont-saint-michel-2.jpg". After remove 's' from the second url like "http://dev.citylaneapp.com/wp-content/uploads/2016/09/france-mont-saint-michel-2.jpg" its working fine. But i dnt want to remove 's' from the url. I want to download image from the second url. So could you please suggest me any idea to do this?
when I try to open the urls in the browser, the second one
gives that warning, mostly it's a server issue that the server claims it's secured but probably it's SSL Certificates aren't installed, you should contact the server admin.

Display image in image view in android application

I got json data in this way:
{"data":[{"img_name":"abc.jpeg","img_path":"6"}]}
Now How can I display this image in image view in my android application.Please help me to solve.
Not sure if this is what you want.
First you need to parse the JSON and get the img_path and img_name. Then you can create the url of the image. The url generation logic depends on your server logic.
Something like this,
JSONObject data = new JSONObject(your_json_data);
String img_path = data.getString("img_path");
String img_name = data.getString("img_name");
String url = "http://xxx.xxx.x.x/" + img_path + "/" + img_name;
Use an imageloader like Picasso to load the image.

save image in a folder on your website without fileupload

i have my website hosted on a server and i have a folder their named images.I am recieving a base64 string and convert it into an image and saving it in my local directory and it works perfectly.
[WebMethod]
public void UploadPics(String imageString)
{
//HttpRequest Request = new HttpRequest();
//HttpPostedFile filePosted = new HttpPostedFile();
string base64String = imageString;
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
//string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
string filePath = "C:/Users/MUWebServices/App_Code/images/pic1.jpg";
image.Save(filePath, ImageFormat.Jpeg);
}
But when i use
string filePath = "http://mywebsite.com/images/pic1.jpg";
image.Save(filePath, ImageFormat.Jpeg);
received following exception
"System.ArgumentException: URI formats are not supported" .
How can i save images on website folder. I found some solutions but all were using fileUpload and i can't use that because i am receiving base64 image string from android and using this webservice to save images.
You will have to upload the file. Since you are opening a TCP/IP connection to another computer you need to follow protocol to write that file to that remote directory. Let me explain this to you as Robert A. Heinlein
once said,
Anyone who considers protocol unimportant has never dealt with a cat. -Robert A. Heinlein
Jokes apart you still have to use file upload(you could do this via AsyncTask. A remote computer will most probably not use a uri for the same.

File size is always zero when images uploaded to google cloud storage from Android using JSON api

I am using the below code snippet to upload an image to google cloud storage from Android using JSON api. Although I have tested the file size before upload ,which is not zero, the file size becomes zero after upload.
InputStream is = getContentResolver().openInputStream(imageUri); //imageUri is obtained after the image is chosen
Storage storage = getStorage();
//The below three lines are a test to get the file size
Bitmap selectedImage1 = BitmapFactory.decodeStream(is);
byte[] imageByte = Utilities.getBytesFromBitmap(selectedImage1);
int size = imageByte.length;
StorageObject object = new StorageObject().setName("myobject")
.setContentDisposition("attachment");
String contentType = "image/jpeg";
InputStreamContent content = new InputStreamContent(contentType, is);
content.setRetrySupported(true);
Storage.Objects.Insert insert = storage.objects().insert("mybucket.appspot.com", object, content); //bucketname changed
insert.execute();
I have taken a look at most of the related questions in stackoverflow but could not figure out a solution yet.

android-where to store lots of images and how to move them while installing

I'm writing an android app that contains about 500 images .
there are somethings that make me worry, I don't want to use internet.
1-the application size will be very big , is there anyway to moving images to sd card while installing? some devices may don't have this amount of space on the phone .
2-should I make 3 images for hdpi , ldpi and mdpi ?
You can put you image in asset folder. If you want to transfer image from assets to SD Card then you can't do like this.
But you can do by one way. You put your image on server and at 1st time when you will open app you can download it and save it in SD Card and then access from there.
Yes, it will be big. No, you can't remove them from your package.
No, you can make only hdpi images. Android will scale them automatically (which may slow down a bit the app).
Suggestion - use internet. Since the user has internet to download your app, he can wait to download the resources on first start. Also it give you the ability to add/remove files via online configuration. Just imagine if you have to add 1 image and upload new version - this means that the user will have to download the same huge package again.
I had a similar requirement - include a bunch of images in the app, but in my case, the image had to be accessible by any user or app, not just the app that unpacked them. I stored them in the res/raw folder and copied them to user space on start up:
private void loadCopyResources() {
// copy resources to space any activity can use
String sourceName;
String resourceName;
String fileName;
int resource;
String typeName = sourceSink.Types.photo.toString();
for (sourceSink.Sources source: sourceSink.Sources.values() ){
for (int i = 0; i< photoFileCount; i++) {
sourceName = source.toString();
resourceName = sourceName + "_" + typeName + (i+1); // i.e. dropbox_photo2
fileName = resourceName + ".jpg"; // files requires extension
resource = getResources().getIdentifier(resourceName, "raw", "com.example.myapp");
createExternalStoragePublicFile(typeName,fileName, resource); // copy it over
}
}
}
void createExternalStoragePublicFile(String fType, String fname, int res ) {
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path = null;
if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())) ) ){
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
}
if (fType.equals(sourceSink.Types.music.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
}
if (fType.equals(sourceSink.Types.video.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
}
File file = new File(path, "/" + fname);
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(res);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
scanMedia(file);
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
sourceSink, which I didn't include, is just a list of file names and file types I needed copied.

Categories

Resources