Image Url is not valid in Firebase Realtime Database - android-studio

I am trying to store the Url of Image from Firebase Storage to Real-time database, and then load the Url by Picasso library, but the image is stored but incorrect formate, all other images are loading but one child with name postimage is not loading because it's URL is not correct.
I only need to know that this code is correct with androidx because I am using androidx.
and rest of things are working properly something is wrong with this code.
Loading Image url from Storage to Realtime database Code:
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful())
{ filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
final String downloadUrl = uri.toString();
postImageUri = downloadUrl;
}
});
PostImage = postImageUri;
// downloadUrl = task.getResult().getUploadSessionUri().toString();
// final String downloadUrl = filePath.getDownloadUrl().toString();
// postImageUri = downloadUrl;
// downloadUrl = task.getResult().getDownloadUrl().toString();
Toast.makeText(PostActivity.this, "Image Uploaded Successfully To Storage!", Toast.LENGTH_SHORT).show();
SavingPostInformationToDatabase( );
}
else
{
String message = task.getException().getMessage();
Toast.makeText(PostActivity.this, "Error! " + message, Toast.LENGTH_SHORT).show();
}
}
});```

I was facing this issue but later on, I solved... as you guys can see that I am using filePath.getDownload method and there I am getting image Uri to store in the realtime database through the variable postImagUri..so as long as the variable inside filePath.getDownload it has the image link properly but as far we use that variable outside that filePath.getDownload it gives a null value that is inappropriate.
So simply I cut my method SavingPostInformationToDatabase( ); and pasted it inside filePath.getDownload Cause the variable that stores image link I was using in SavingPostInformationToDatabase( ); method.
SavingPostInformationToDatabase( ); is a method that stores values to realtime database using HashMap method.

Related

Authentication Failure when uploading to Azure Blob Storage using Azure.Storage.Blob v12.9.0

I get this error when trying to upload files to blob storage. The error is present both when I run on localhost and when I run in Azure Function.
My connection string looks like:
DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net
Authentication information is not given in the correct format. Check the value of the Authorization header.
Time:2021-10-14T15:56:26.7659660Z
Status: 400 (Authentication information is not given in the correct format. Check the value of Authorization header.)
ErrorCode: InvalidAuthenticationInfo
But this used to work in the past but recently its started throwing this error for a new storage account I created. My code looks like below
public AzureStorageService(IOptions<AzureStorageSettings> options)
{
_connectionString = options.Value.ConnectionString;
_containerName = options.Value.ImageContainer;
_sasCredential = new StorageSharedKeyCredential(options.Value.AccountName, options.Value.Key);
_blobServiceClient = new BlobServiceClient(new BlobServiceClient(_connectionString).Uri, _sasCredential);
_containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
}
public async Task<string> UploadFileAsync(IFormFile file, string location, bool publicAccess = true)
{
try
{
await _containerClient.CreateIfNotExistsAsync(publicAccess
? PublicAccessType.Blob
: PublicAccessType.None);
var blobClient = _containerClient.GetBlobClient(location);
await using var fileStream = file.OpenReadStream();
// throws Exception here
await blobClient.UploadAsync(fileStream, true);
return blobClient.Uri.ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
// To be able to do this, I have to create the container client via the blobService client which was created along with the SharedStorageKeyCredential
public Uri GetSasContainerUri()
{
if (_containerClient.CanGenerateSasUri)
{
// Create a SAS token that's valid for one hour.
var sasBuilder = new BlobSasBuilder()
{
BlobContainerName = _containerClient.Name,
Resource = "c"
};
sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
sasBuilder.SetPermissions(BlobContainerSasPermissions.Write);
var sasUri = _containerClient.GenerateSasUri(sasBuilder);
Console.WriteLine("SAS URI for blob container is: {0}", sasUri);
Console.WriteLine();
return sasUri;
}
else
{
Console.WriteLine(#"BlobContainerClient must be authorized with Shared Key
credentials to create a service SAS.");
return null;
}
}
Please change the following line of code:
_blobServiceClient = new BlobServiceClient(new BlobServiceClient(_connectionString).Uri, _sasCredential);
to
_blobServiceClient = new BlobServiceClient(_connectionString);
Considering your connection string has all the necessary information, you don't really need to do all the things you're doing and you will be using BlobServiceClient(String) constructor which expects and accepts the connection string.
You can also delete the following line of code:
_sasCredential = new StorageSharedKeyCredential(options.Value.AccountName, options.Value.Key);
and can probably get rid of AccountName and Key from your configuration settings if they are not used elsewhere.

Pass Image Bitmap to Azure Face SDK detectWithStream()

I am trying to write a React app that grabs a frame from the webcam and passes it to the Azure Face SDK (documentation) to detect faces in the image and get attributes of those faces - in this case, emotions and head pose.
I have gotten a modified version of the quickstart example code here working, which makes a call to the detectWithUrl() method. However, the image that I have in my code is a bitmap, so I thought I would try calling detectWithStream() instead. The documentation for this method says it needs to be passed something of type msRest.HttpRequestBody - I found some documentation for this type, which looks like it wants to be a Blob, string, ArrayBuffer, or ArrayBufferView. The problem is, I don't really understand what those are or how I might get from a bitmap image to an HttpRequestBody of that type. I have worked with HTTP requests before, but I don't quite understand why one is being passed to this method, or how to make it.
I have found some similar examples and answers to what I am trying to do, like this one. Unfortunately they are all either in a different language, or they are making calls to the Face API instead of using the SDK.
Edit: I had forgotten to bind the detectFaces() method before, and so I was originally getting a different error related to that. Now that I have fixed that problem, I'm getting the following error:
Uncaught (in promise) Error: image must be a string, Blob, ArrayBuffer, ArrayBufferView, or a function returning NodeJS.ReadableStream
Inside constructor():
this.detectFaces = this.detectFaces.bind(this);
const msRest = require("#azure/ms-rest-js");
const Face = require("#azure/cognitiveservices-face");
const key = <key>;
const endpoint = <endpoint>;
const credentials = new msRest.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } });
const client = new Face.FaceClient(credentials, endpoint);
this.state = {
client: client
}
// get video
const constraints = {
video: true
}
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
let videoTrack = stream.getVideoTracks()[0];
const imageCapture = new ImageCapture(videoTrack);
imageCapture.grabFrame().then(function(imageBitmap) {
// detect faces
this.detectFaces(imageBitmap);
});
})
The detectFaces() method:
async detectFaces(imageBitmap) {
const detectedFaces = await this.state.client.face.detectWithStream(
imageBitmap,
{
returnFaceAttributes: ["Emotion", "HeadPose"],
detectionModel: "detection_01"
}
);
console.log (detectedFaces.length + " face(s) detected");
});
Can anyone help me understand what to pass to the detectWithStream() method, or maybe help me understand which method would be better to use instead to detect faces from a webcam image?
I figured it out, thanks to this page under the header "Image to blob"! Here is the code that I added before making the call to detectFaces():
// convert image frame into blob
let canvas = document.createElement('canvas');
canvas.width = imageBitmap.width;
canvas.height = imageBitmap.height;
let context = canvas.getContext('2d');
context.drawImage(imageBitmap, 0, 0);
canvas.toBlob((blob) => {
// detect faces
this.detectFaces(blob);
})
This code converts the bitmap image to a Blob, then passes the Blob to detectFaces(). I also changed detectFaces() to accept blob instead of imageBitmap, like this, and then everything worked:
async detectFaces(blob) {
const detectedFaces = await this.state.client.face.detectWithStream(
blob,
{
returnFaceAttributes: ["Emotion", "HeadPose"],
detectionModel: "detection_01"
}
);
...
}

How to download an Excel file that has been opened by Google Sheets?

I've been running into issues when downloading Excel .xlsx files using Google Drive Api v3. The code I'm using is as follows (I'm using the .NET SDK):
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace DriveQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.Drive };
static string ApplicationName = "Drive API .NET Quickstart";
const string FileId = "my_file_id"; //put the ID of the Excel file you want to download here
public static void Main(string[] args)
{
Run().GetAwaiter().GetResult();
Console.Read();
}
private static async Task Run()
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.GetRequest getRequest = service.Files.Get(FileId);
using (var stream = new System.IO.FileStream("anExcelFile.xlsx", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
{
var downloadProgress = await getRequest.DownloadAsync(stream, CancellationToken.None);
if (downloadProgress.Exception != null)
{
Console.WriteLine(string.Format("We got error {0} {1} {2}", downloadProgress.Exception.Message, Environment.NewLine, downloadProgress.Exception.StackTrace));
}
else
{
Console.WriteLine("Download ok");
}
}
}
}
}
You can run this sample easily by following the steps described here. This works fine, however, as soon as someone opens the file with Google Sheets and modifies it, I start seeing the following error
D2020-03-16 02:10:13.647293 Response[00000007] Response status: InternalServerError 'Internal Server Error'
D2020-03-16 02:10:13.653278 Response[00000007] An abnormal response wasn't handled. Status code is InternalServerError
D2020-03-16 02:10:13.660288 Response[00000007] Abnormal response is being returned. Status Code is InternalServerError
E2020-03-16 02:10:13.667240 Exception occurred while downloading media The service drive has thrown an exception: Google.GoogleApiException: Internal Server Error
at Google.Apis.Download.MediaDownloader.<DownloadCoreAsync>d__31.MoveNext()
Looking at the file info after it was open with Google sheets, I can see that its size is changed to 0, so I tried to export it as you would for a Google spreadsheet, like so:
FilesResource.ExportRequest exportRequest = client.Files.Export(fileId, mimeType);
using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
{
await exportRequest.DownloadAsync(stream, cancellationToken);
}
With mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
However, I then run in the following error:
D2020-03-16 01:53:13.512928 Response[00000003] Response status: Forbidden 'Forbidden'
D2020-03-16 01:53:13.520906 Response[00000003] An abnormal response wasn't handled. Status code is Forbidden
D2020-03-16 01:53:13.525911 Response[00000003] Abnormal response is being returned. Status Code is Forbidden
E2020-03-16 01:53:13.538857 Exception occurred while downloading media The service drive has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Export only supports Google Docs. [403]
Errors [
Message[Export only supports Google Docs.] Location[ - ] Reason[fileNotExportable] Domain[global]
]
at Google.Apis.Download.MediaDownloader.<DownloadCoreAsync>d__31.MoveNext()
So it seems that neither downloading nor exporting is working in this particular case. Anything else I should be trying ? Using the webContentLink (https://drive.google.com/uc?id=fileId&export=download) works fine (in a browser that is) so I guess it should be possible to download the file.
I raised the issue to Google and it seems it was fixed (cf this issue). I tried again today and following the steps described in the original question, I now can see that after the Excel file has been edited with Google sheets, its size is now greater than 0 and it can be downloaded.
Files that couldn't be downloaded because of this issue still appear to have the same problem but deleting/reuploading manually these files should make them downloadable.

Cloudinary recal unsigned images url

i have some kind of issue implanting unsigned uploaded images url recal, the way it is mentioned in this page:
http://cloudinary.com/documentation/java_image_upload
does not go well with the method i used to upload unsigned unsigned :
#Override
protected Void doInBackground(String... params) {
Map config = new HashMap();
config.put("cloud_name", "we4x4");
Cloudinary cloudinary = new Cloudinary(config);
try {
cloudinary.uploader().unsignedUpload((""+ RealFilePath), "frtkzlwz",
Cloudinary.asMap( "tags", UserID,"resource_type", "auto"));
} catch (IOException e) {
e.printStackTrace();
progressDialog.setMessage("Error uploading file");
progressDialog.hide();
}
return null;
}
could someone explain to me how and where do i write the code to get and address of the uploaded images ?
i am using android studio.
I was able to upload the a file, and recall its address using the following code, but when i try to substitute .upload with .unsignedUpload as i used before to upload without my full config, the syntax get underlined red ? tried several ways to patch it but not working ? i would appreciate some tips on the right syntax to achieve this ?
Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
"cloud_name", "we4x4",
"api_key", "xxxxxxxxxxxxx",
"api_secret", "xxxxxxxxxxxxxxxx"));
try{
Map result = cloudinary.uploader().upload("" + RealFilePath, ObjectUtils.asMap(
"tags", UserID));
uploadedContentURL = (String) result.get("url");
The unsigned_upload() method expects the following attributes: file, UploadPreset & options Map, unlike the upload() api that doesn't require the uploadPreset parameter.
However, both return a response from the server formed as JSONObject.
There you can find all the information required for generating the URL (e.g. public_id, format, version, etc.)
A code example is available here: https://github.com/cloudinary/cloudinary_java/blob/master/cloudinary-android-test/src/main/java/com/cloudinary/test/UploaderTest.java#L67

Xamarin: Issues with transitioning between controllers and storyboard views

I realize this might come across as a very basic question, but I just downloaded Xamarin three days ago, and I've been stuck on the same issue for two days without finding a solution.
Here is what I am trying to do:
Get user input, call API, parse JSON and pass data to another controller, and change views.
Here is what I have been able to do so far: I get the user input, I call the API, I parse the response back, write the token to a file, and I want to pass the remaining data to the second controller.
This is the code I am trying:
var verifyCode = Storyboard.InstantiateViewController("verify") as VerifyCodeController;
if (verifyCode != null)
{
this.NavigationController.PushViewController(verifyCode, true);
}
My storyboard setup:
Navigation controller -> routesTo -> FirstController
I have another UI Controller View set up with the following properties set:
storyboardid: verify
restorationid: verify
and I am trying to push that controller view onto the navigation controller.
Right now this line is erroring out:
var verifyCode = Storyboard.InstantiateViewController("verify") as VerifyCodeController;
giving me this error, which I don't know what it means: Could not find an existing managed instance for this object, nor was it possible to create a new managed instance.
Am I way off in my approach?
p.s: I cannot use the ctrl drag thing like the tutorial suggests because I have an asynchronous call. And I cannot under no circumstances make it synchronous. So all the page transition has to be manual.
EDIT
to anyone requesting more code or more info:
partial void registerButton_TouchUpInside (UIButton sender)
{
phone = registrationNumber.Text;
string url = url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
request.Method = "GET";
Console.WriteLine("Getting response...");
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
}
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string content = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(content))
{
//Console.WriteLine(text);
Console.Out.WriteLine("Response contained empty body...");
}
else
{
var json = JObject.Parse (content);
var token = json["token"];
var regCode = json["regCode"];
var aURL = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (aURL, "app.json");
File.WriteAllText(filename, "{token: '"+token+"'}");
// transition to main view. THIS IS WHERE EVERYTHING GETS MESSED UP
var verifyCode = Storyboard.InstantiateViewController("verify") as VerifyCodeController;
if (verifyCode != null)
{
this.NavigationController.PushViewController(verifyCode, true);
}
}
}
}
}
Here is all the info for every view in my storyboard:
1- Navigation controller:
- App starts there
- The root is the register pager, which is the page we are currently working on.
2- The register view.
- The root page
- class RegisterController
- No storyboard id
- No restoration id
3- The validate view
- Not connected to the navigation controller initially, but I want it to be connected eventually. Do I have to connect it either way? Through a segue?
- class VerifyCodeController
- storyboard id : verify
- restoration id : verify
If you guys need more information I'm willing to post more. I just think I posted everything relevant.

Resources