Exporting contact activity from an android phone - android

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.

Related

How to find files quickly?

I really need some help please!
I want to make a MP3 Player on Android and therefore I need to find all MP3 files in the storage.
So I search them recursively in my get MP3 method:
private void getMP3Files(Context context, String directory,ArrayList<MusicListArray> mp3_list){
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
Uri uri;
byte[] album_art;
Bitmap bitmap;
/*File[] files = Directory.listFiles(new MP3FileNameFilter());
files = Directory.listFiles();*/
File folder = new File(directory);
for (File file : folder.listFiles()) {
if (file.isFile()) {
if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
uri = Uri.fromFile(file);
mediaMetadataRetriever.setDataSource(context,uri);
String artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = file.getName();
album_art = mediaMetadataRetriever.getEmbeddedPicture();
if(album_art != null){
bitmap = BitmapFactory.decodeByteArray(album_art, 0, album_art.length);
}else{
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
}
if(bitmap == null){
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
}
if (title.indexOf(".") > 0)
title = title.substring(0, title.lastIndexOf("."));
if(artist == null){
artist = getString(R.string.unknown_artist);
}
mp3_list.add(new MusicListArray(title,artist,file,bitmap));
}
}else if(file.isDirectory()){
getMP3Files(context, file.getPath(), mp3_list);
}
}
Collections.sort(mp3_list);
/*for(int i=0;i<files.length;i++){
mp3_list.add(new MusicListArray(files[i].getName(),"Test",files[i]));
}*/
//return mp3_list;
}
This is very slow and needs about 3 seconds everytime I start my app.
First question: How do I manage to reduce the taken time?
Second question: How to save the list in a file and load it?
Thanks in advance!
I suggest you use an SQL database to store the metadata (including file location) of your mp3s. I should warn you that I have no experience with media on Android though. Basic Idea:
1) First Time use (or on a manual sync in settings) do a full 3 second sync -> look at SharedPreferences to store a key indicating first time use for your app
2) Store this meta information in a SQL database.
3) Make the app read from the database as this is much faster, especially if the table is designed correctly. Store things like cover art, file location, artist, title etc. You can also add things like number of times listened and stuff. In addition if you wrap your SQL layer into a content provider you can perform custom searches of all your music.
4) When app is running, run a background indexing service that finds new music, corrects changes/deletes etc. Look into android Service
The benefits of this is that your app will be much faster to the user, only need to load all the data once in the foreground and provide you with a lot more flexibility in terms of searching, song choice, custom data etc.

Is it possible to retrieve album art from remote mp3 file in Android?

I'm currently writing a UPnP remote control app which is used to connect a remote MediaServer to a remote MediaRenderer. Since the actual MP3 files aren't sent to the Android device, I'd like to be able to get the album art of the currently playing file without having to download the entire MP3 file to my phone.
I've read that MediaMetadataRetriever is useful for this kind of thing, but I haven't been able to get it to work. Each way I try it, I keep getting an IllegalArgumentException by the call to MediaMetadataRetriever#setDataSource, which indicates that my file handle or URI is invalid.
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
The following works since it's a direct file path on the device itself:
metaRetriever.setDataSource("/sdcard/Music/Daft_Punk/Homework/01 - Daftendirekt.mp3");
However, any of the following fail with the same error:
metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/1/rct/aa"));
metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3"));
metaRetriever.setDataSource("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3");
The first one is the albumArtURI pulled from the UPnP metadata (no *.mp3 extension, but the file will download if pasted into a web browser).
The second and third attempts are using the "res" value from the UPnP metadata, which points to the actual file on the server.
I'm hoping I'm just parsing the URI incorrectly, but I'm out of ideas.
Any suggestions? Also, is there a better way to do this entirely when pulling from a UPnP server? FWIW, I'm using the Cling UPnP library.
== SOLUTION ==
I started looking into william-seemann's answer and it led me to this: MediaMetadataRetriever.setDataSource(String path) no longer accepts URLs
Comment #2 on this post mentions using a different version of setDataSource() that still accepts remote URLs.
Here's what I ended up doing and it's working great:
private Bitmap downloadBitmap(final String url) {
final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(url, new HashMap<String, String>());
try {
final byte[] art = metaRetriever.getEmbeddedPicture();
return BitmapFactory.decodeByteArray(art, 0, art.length);
} catch (Exception e) {
Logger.e(LOGTAG, "Couldn't create album art: " + e.getMessage());
return BitmapFactory.decodeResource(getResources(), R.drawable.album_art_missing);
}
}
FFmpegMediaMetadataRetriever will extract metadata from a remote file (Disclosure: I created it). I has the same interface as MediaMetadataRetriever but it uses FFmpeg as it's backend. Here is an example:
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
String album = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
byte [] artwork = mmr.getEmbeddedPicture();
mmr.release();
Looking at the source code for MediaMetadataRetriever (not from the official Android repo, but it should still be similar, if not equivalent) showed me this:
if (uri == null) {
throw new IllegalArgumentException();
}
And this:
ContentResolver resolver = context.getContentResolver();
try {
fd = resolver.openAssetFileDescriptor(uri, "r");
} catch(FileNotFoundException e) {
throw new IllegalArgumentException();
}
if (fd == null) {
throw new IllegalArgumentException();
}
FileDescriptor descriptor = fd.getFileDescriptor();
if (!descriptor.valid()) {
throw new IllegalArgumentException();
}
Your exception is coming from one of those blocks.
From looking at the MediaMetadataRetriever documentation and source code, it seems to me that the file has to be on the device. You can use a Uri, but I think it has to be something like "file:///android_asset/mysound.mp3". I could be wrong though; are you sure that MediaMetadataRetriever can be used to resolve files over a network?

Android: how to get own profile picture like in native Messaging app

I'm writing an application for SMS messaging and I want to show profile picture of all senders (it is not a problem) and picture of my profile for sent messages. The same implementation was done in native Messaging app.
Could you please advice how I can get own profile picture?
I found a solution for my question:
public static Bitmap getProfilePhoto(Context context) {
Bitmap profilePhoto = null;
ContentResolver cr = context.getContentResolver();
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, ContactsContract.Profile.CONTENT_URI);
if (input != null) {
profilePhoto = BitmapFactory.decodeStream(input);
} else {
profilePhoto = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture);
}
return profilePhoto;
}
Read up here
http://developer.android.com/reference/android/provider/ContactsContract.Profile.html
and then
look into CursorLoader's and cursors, write one to query for the photo uri or photo thumbnail uri.
Update
I found similar questions that should help you
Get profile picture of specific phone number from contacts information
Get user/owner profile contact URI and user image with API 8 onwards

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.

Fetching attachments with custom extension in my Android application

What I am trying to achieve is sounds very familiar, it has been posted many times here and there in Stack Overflow as well, but I'm unable to get it done.
The scenario is, I receive a mail with attachment having custom extension in it. The extension is recognized by my app and it needs the FilePath to process it.
Currently, when I get the attachment in my app using getIntent().getData() all I get is path of the form content://
I have seen methods to convert media content of the type content:// to FilePath like /sdcard/file.ext but I was unable to convert the attachment using that. May be its obvious.
Is there any way that I can process the content:// type without actually downloading it.
Currently from the k9 mail app, when I get the custom extension, it shows my app in the list and opens it through it, but I need FilePath like /sdcard/file.ext and I'm only able to get content:// type.
I hope I made the question clear.
Please Help.
Regards.
A content:// Uri does not necessarily point to a file on the sdcard.
It is more likely that it points to any kind of data stored in a database
or to a content provider that gives you access to the private file storage of another app.
I think the later one is the case with mail attachments (if the content provider is not requesting it directly from a web server). So converting the content:// Uri to a path will not work.
I did the following (not sure if it works also for k9 mail app)
Uri uri = intent.getData();
if (uri.getScheme().equals("content")) {
String fileName = ContentProviderUtils.getAttachmentName(this, uri);
if (fileName.toLowerCase().endsWith(".ext")) {
InputStream is = this.getContentResolver().openInputStream(uri);
// do something
} else {
// not correct extension
return;
}
} else if (uri.getScheme().equals("file")) {
String path = uri.getPath();
if (path.toLowerCase().endsWith(".ext")) {
InputStream is = new FileInputStream(path);
// do something
} else {
// not correct extension
return;
}
}
The attachment name can be found by
public static String getAttachmentName(Context ctxt, Uri contentUri) {
Cursor cursor = ctxt.getContentResolver().query(contentUri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
String res = "";
if (cursor != null){
cursor.moveToFirst();
int nameIdx = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
res = cursor.getString(nameIdx);
cursor.close();
}
return res;
}

Categories

Resources