ParseObject save locally. Error: Unable to encode an unsaved parse file - android

I need to save ParseObject with ParseFile, but locally. Method pinInBackground gives error: "Unable to encode an unsaved parse file"
I can not call file.saveInBackground. Because I need to use offline mode.
So what should I do?
//get bitmap
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] data = baos.toByteArray();
Random random = new Random();
//create parse File
final ParseFile file = new ParseFile(random.nextInt(10000) + ".jpeg", data);
parseObject.put(KEY_IMAGE, file);
parseObject.pinInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
//do some action
}
});

Let me create an answer for all of this.
That error really does tell you what is wrong with the app. You can not pin something that isn't there. Parse pins objects.... you never created an object (yet). Therefore, you can not pin a unsaved object.
You can do two things.
1) If there is no internet connection, display the message to the user and tell them that. Once they restore, they can try again.
2) Use saveEventually. This will save the object once internet hits the device, and then when done, you can pin it. Problem is, if the object doesn't exist and the user wants to see it, they cant.
If it was me, I would go with option 1. If you are asking your user to input files, some sort of connection would be required.

Related

How do we extract the bigpicturestyle image from android notifications?

I have a notification listener service that reads notifications from other apps (with the user's permission) and extracts all the data. Able to access everything except the image shown in the expanded view of the notification.
I am also reading the EXTRA_PICTURE intent value
if (extras.containsKey(Notification.EXTRA_PICTURE)) {
// this bitmap contain the picture attachment
try {
Bitmap bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
ByteArrayOutputStream picStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, picStream);
byte[] picBmpData = picStream.toByteArray();
notificationPicture = Base64.encodeToString(picBmpData, Base64.NO_WRAP);
} catch(Exception e){
e.printStackTrace();
}
}
This works sometimes but for all notifications with image in their expanded view. Am I missing anything?
UPDATE: more clarification on what happens in the negative cases:
In negative cases when there is an image in the notification, the extras dont seem to have the EXTRA_PICTURE key set. However I do see
android.template
key set to
android.app.Notification$BigPictureStyle

Exporting contact activity from an android phone

Im interested in running some custom analytics on my interactions with contacts on my phone.
Some things I would like to see are:
How many times was this contact called
How long did the conversation last
How many missed calls from this contact
How many answered calls from this contact
What time and date was an sms sent
What was its message content
What time and date was an sms received
What was its message content
If it was mms can i get the picture some how
Ill use a third party api for facial recognition and nudity checks (was it a nude, selfie, meme)
Is there a way to simply export this data into a xml or csv file? (How would I save pictures?)
My goal here is to make an app using some sort of android java sdk. Then using the app, ill upload to my web server and use php to do the analytics.
Where do i look to start getting the information i want to analyze?
Try to look at these links:
PhoneStateListener
TelephonyManager
Read SMS
ContentResolver
To export your pictures from mms use a filestream and a bitmap:
private void GetMmsAttachment(String _id, String _data)
{
Uri partURI = Uri.parse("content://mms/part/" + _id );
String filePath = "/sdcard/photo.jpg";
InputStream is = null;
OutputStream picFile = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
picFile = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, picFile);
picFile.flush();
picFile.close();
}
catch (Exception e)
{
e.printStackTrace();
//throw new MmsException(e);
}
}
Also for photo Identification I recommend you use IBM Watson,.If the photo has text use Google Tesseract to extract the text.

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.

Extract exif information of image in android

Im new to android development and trying to get metadata of image using ExifInterface. I stored the image under drawable and trying to get the metadata but getting null values for all fields(date, imagelength, imagewidth). I tried to access image path as this :
String path = "drawable://" + R.drawable.testimage;
and provided this path to ExifInterface.
ExifInterface exif = new ExifInterface(path);
I dont know if storing image under drawable is correct or not because when I run the app in emulator I get something like this :
E/JHEAD﹕ can't open 'drawable://2130837561'
So if this is wrong then please tell me where should I store the image and how to provide image path to ExifInterface.
Thank you in advance.
To get a drawable, you can you this snippet:
Drawable drawable = getResources().getDrawable(android.R.drawable.your_drawable);
I'm not sure if your way is correct, as I've never seen it like that. Do you really need the path to your image to use it on that ExifInterface class?
Ok, I did some digging and found this question, which led me to this one. As it seems, you can not get an absolute path from a resource inside your apk. A good solution would be for you to save it as a file on the external memory, and then you can get the path you want.
First of all, add this to your AndroidManifest.xml, so your app can write to the cellphone memory:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ok, to save it you can try this, first create a bitmap from your drawable resource:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.your_drawable);
After that get the path you want to save your images, and put it on a String. More info on that here.
The Android docs have a good example on how to get the path. You can see it here.
To keep it simple, I'll copy and paste the snippet from the docs.
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
// 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(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
void deleteExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
file.delete();
}
}
boolean hasExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
return file.exists();
}
return false;
}
After that, get the path of the file you saved on the external memory, and do as you wish.
I'll keep the old example as well. You can use the method getExternalStorageDirectory() to get the path, or getExternalCacheDir(). After that, you can use File method called getAbsolutePath() to get your String.
String path = (...) // (you can choose where to save here.)
File file = new File(path, "your_drawable.png");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // You can change the quality from 0 to 100 here, and the format of the file. It can be PNG, JPEG or WEBP.
out.flush();
out.close();
For more info on the Bitmap class, check the docs.
If you need more info, let me know and I'll try to show more samples.
EDIT: I saw your link, and there was this snippet there:
//change with the filename & location of your photo file
String filename = "/sdcard/DSC_3509.JPG";
try {
ExifInterface exif = new ExifInterface(filename);
ShowExif(exif);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Error!",
Toast.LENGTH_LONG).show();
}
As you can see, if you really want to see the exif data of a internal image resource, you'll have to save it somewhere else, and then you can try to get the absolute path for that File, then, call the method to show the exif.

How to programmatically include link to a facebook page (Android)?

My app has a feature that posts a photo + text to a user's facebook wall (works fine). Now I'm trying to include a link in the text that goes to a specific facebook page (doesn't work).
The basic code looks like this (works fine):
private void postImageToFacebookWall(String filePath, String msg) {
try {
Bundle param = new Bundle();
param = new Bundle();
// prep photo byte array
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// add byte array and user msg
param.putByteArray("image", byteArray);
param.putString("message", msg);
// post to Facebook
mAsyncRunner.request("me/photos", param, "POST", new PostRequestListener(), null);
} catch (Exception e) {
e.printStackTrace();
}
}
Now I'm trying to embed a link to a facebook page in the msg, using the following syntax:
#[fb_page_id:str]
This works when I type it directly into facebook. But it doesn't work when I use it in the code, modified as follows (doesn't work):
String fbPageRef = "#[" + Constants.FACEBOOK_PAGE_ID + ":str]";
param.putString("message", msg + " " + fbPageRef);
When I run the code with the embedded link (fbPageRef), it doesn't show up.
What am I doing wrong? Thanks.
I didn't notice at first that a facebook post generated by an app already includes an attribution to the source app. It's at the bottom of the post and reads something like: "2 hours ago via YourAppName".
This is friendly to read, and correctly links back to the app's facebook page (assuming there is one). So if that's good enough, you don't have to worry about how to insert a link in the text part of the post!
Still it would be good to know how to embed a link to a facebook page in a machine-posted message (?). Thanks.

Categories

Resources