I'm trying to get a users facebook profile pic using the FB api.
My way of doing this isn't working... first I make a request like this:
String str = facebook.request("me/picture");
Next I create an inputstream from the string str so I can decode it with a bitmapfactory
InputStream is = new ByteArrayInputStream(str.getBytes());
Bitmap bm = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bm);
This never displays the image!
Haven't been able to find much about this. Any help would be appreciated.
I fixed this by just using HTTPGet to the url The first time I tried it I got a 404 error but when I rewrote it it worked!
Related
I am trying to download image using an url like:
url --> http://www.example.com/path/to/image-ğüçöşı.jpg
InputStream input = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
However in the first line app crashes. Because it has a character like "ı" or "ç". If url doesn't has those character it doesn't crash and works fine.
I could almost say i tried most of the solutions like utf8 encoding and etc, including giving "UTF8" params to HttpClient.
It would be appreciated very much if you could help me. I am looking for any solution that doesn't slow down the code very much.
Thank you
Encode your url or only image name with this code
String query = URLEncoder.encode("strangeChars", "utf-8");
I have tried a lot to add icon in connections of contacts of android phone whne that contact is used by my app.But I not able to get even a single reference to do it.Please do the favourable.I tried to use this link
But not get any solution as its not properly explained.Simply I have to add icon like whats app goggle plus in phone contact if its used by my app
This code works pretty well to get a photo, if I understand you correctly:
Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(cr, my_contact_Uri, true);
BufferedInputStream buf = new BufferedInputStream(photo_stream);
Bitmap my_btmp = BitmapFactory.decodeStream(buf);
buf.close();
return my_btmp;
Look at this question: Get high-res contact photo as bitmap below API level 14 Android for more info.
I am trying to show byte array of image from server inside the image view of an android activity. I am able to get the byte array correctly as sent from the server, but while converting it into bitmap it is always returning null. I have used the BitmapFactory.decode(byteArray,0, byteArray.length) for converting image byte array to bitmap which is returning null always.
Please help me solving this and tell me if there are any alternative.
Thanks in advance.
Well a better option is to use this. I used it to display images that is present on a server. Pretty fast
URL url = new URL(link);
InputStream picin = url.openStream();
Bitmap myBitmap = BitmapFactory.decodeStream(picin);
pic.setImageBitmap(myBitmap);
Where link points to the jpg image. Works for other image formats also.
So ultimately I'm trying to upload images that I want Google to OCR. Then I want to be able to get the results of the OCR back to my Android app. I have my images uploading properly. I can loop through all the files in my google drive and I see that there are export links available, one of which is "text/plain". If I use one of these urls in a browser, it downloads the text. Is this the way I should be trying to access it?
I've tried to use the url I get from calling getExportLinks method on the file returned by the insert method
File file = drive.files().insert(body, mediaContent).setOcr(true).execute();
String imageAsTextUrl = getExportLinks.get("text/plain")
I end up getting HTML back that appears to be the Google Drive home page. To get the exported url document, I used google drive instance so it should have properly authenticated like the insert method I would think.
DriveRequest request = new DriveRequest(drive, HttpMethod.GET, imageAsTextUrl, null);
Has anyone tried to do this before? What am I doing wrong?
Well I answered my own question yet again, sort of. Basically since this seems to be a web url and not an API call I can make, then it's not responding with a 401 if it's unauthenticated. So basically the response I was getting is the HTML for the login page. Apparently using DriveRequest does not automatically handle authentication like I thought it would. So I have it working by adding authentication manually to an HttpClient GET call.
But is there a way to do what I'm trying to do with the actual API? So I can deal with response codes?
Here's what I did to download the text/plain representation of the file. Here's a caveat: given that the image I was uploading was taken on a cell phone camera using the default camera app, the default dpi and/or jpeg compression caused the OCR to not work very well. Anyway, here's the code I used. Just basic HttpClient stuff
String imageAsTextUrl = file.getExportLinks().get("text/plain");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(imageAsTextUrl);
get.setHeader("Authorization", "Bearer " + token);
HttpResponse response = client.execute(get);
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
}
finally {
if (in != null) {
in.close();
}
}
// Send data to new Intent to display:
Intent intent = new Intent(UploadImageService.this, VerifyTextActivity.class);
intent.putExtra("ocrText", sb.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am brand new to Android development, and have run into an issue with my first major app that I would very much appreciate help on. The goal of this app is to allow the user to select an image stored on the phone, and upload that image to a server via a http post method, taking the JPEG binary data as a parameter. The catch is that I need to preserve the EXIF data on this as well. I currently have pushed an image onto my emulator's sdcard, and used a basic app to confirm that it is there with the EXIF data.
Here is what I've got so far:
To provide the user with a choice of images, and as a result obtain the image Uri, I use:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);
In my activity result code, I obtain my Uri with:
Uri selectedImage = data.getData();
I use this to get a bitmap of the image and display it to the user as a preview.
Here is where I get stuck, as I'm not sue how best to upload it. I have looked a lot at this example in hopes of doing something similar, but he seems to be just compressing a bitmap and sending it, which would probably destroy the EXIF data I need, plus I can't even seem to get that to work.
This is the code I'm trying right now, which given my low experience with such things may be entirely wrong:
Uri selectedImage = data.getData();
InputStream imageInputStream=this.getContentResolver().openInputStream(selectedImage);
String str=imageInputStream.toString();
byte[] imageBits=str.getBytes();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("where I am sending it");
ByteArrayBody toUpload = new ByteArrayBody(imageBits, "androidpic.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("userImage", toUpload);
reqEntity.addPart("FileName", new StringBody("android pic"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
This of course does not seem to work.
Does anyone have experience uploading jpegs from the phone like this who could give me some pointers? Any help would be greatly appreciated.