public int processMMSes() { int addedMMSes = 0; Cursor cur = null; Cursor addr; Cursor part; FileOutputStream fos; try { cur = myContext .getContentResolver() .query(Uri.parse(Constants.mmsBaseUri), null, null, null, null); while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex("_id")); boolean sent = cur.getInt(cur.getColumnIndex("msg_box")) == MESSAGE_TYPE_SENT; String trid = cur.getString(cur.getColumnIndex("tr_id")); long tstamp = cur.getLong(cur.getColumnIndex("date")); // String subject = cur.getString(cur.getColumnIndex("sub")); String tmpsub = cur.getString(cur.getColumnIndex("sub")); String subject = tmpsub == null ? "" : Utilities.utf8ToInternal(tmpsub); // String subject = new String(tmpsub.getBytes(ISO_8859_1), UTF_8); // String subject = tmpsub; // Log.d(TAG, "subject : "+subject); // LOG.debug("processMMS :: subject : {}", Utilities.utf8ToInternal(tmpsub)); String type = ""; String pathToAttachment = ""; String attachmentName = ""; ArrayList<String> to = new ArrayList<String>(); addr = null; try { addr = myContext .getContentResolver() .query(Uri.parse(Constants.mmsBaseUri + id + "/addr"), null, null, null, null); while (addr.moveToNext()) { String num = addr.getString(addr.getColumnIndex("address")); if (Utilities.isMMSAddressValid(num)) { to.add(num); } } } catch (NullPointerException npe) { LOG.error("doInBackground :: null pointer exception on addr.\n\t", npe); } finally { // Utilities.close(addr); if (addr != null) { addr.close(); } } part = null; try { part = myContext .getContentResolver() .query(Uri.parse(Constants.mmsBaseUri + id + "/part"), null, null, null, null); while (part.moveToNext()) { String _id = part.getString(part.getColumnIndex("_id")); byte[] fileBytes = Utilities.getMMSPart(myContext, _id); if (fileBytes != null) { LOG.debug("doInBackground :: mms part found."); type = part.getString(part.getColumnIndex("ct")); // pathToAttachment = part.getString(part.getColumnIndex("_data")); attachmentName = part.getString(part.getColumnIndex("name")); // File workingFolder = new File(myDir); // TODO CREATE FOLDER TREE ONLY IF PART IS NOT NULL, CHECK GETMMSPART RESULT FIRST File relative = new File(trid, attachmentName); // Log.d(TAG, "relative path : "+relative.getPath()); File absolute = new File(myDir, relative.getPath()); new File(myDir, trid).mkdirs(); /*File folder = new File(myDir, trid); folder.mkdirs();*/ // absolute.mkdir(); // Log.d(TAG, "path : "+absolute.getPath()); // fos = null; try { fos = new FileOutputStream(absolute); fos.write(fileBytes); } catch (FileNotFoundException fne) { // Log.d(TAG, "doInBackground :: FileNotFoundException :: "+fne.getMessage()); LOG.debug("doInBackground :: FileNotFoundException.\n\t", fne); } catch (IOException ioe) { // Log.d(TAG, "doInBackground :: IOException :: "+ioe.getMessage()); LOG.debug("doInBackground :: IOException.\n\t", ioe); } pathToAttachment = relative.getPath(); } else { LOG.debug("doInBackground :: no mms part found"); } } } catch (NullPointerException npe) { LOG.error("doInBackground :: null pointer exception on part.\n\t", npe); } finally { // Utilities.close(part); if (part != null) { part.close(); } } MMS mms = new MMS( trid, tstamp, to.toArray(new String[to.size()]), pathToAttachment, attachmentName, subject, type, sent); if (mms.isValid()) { myMMSes.add(mms); // Log.d(TAG, "cursor : \n" + cur.toString()); // publishProgress(new ProgressUpdate(myMMSBar, addedMMSes += 1)); addedMMSes += 1; } // publishProgress(new ProgressUpdate(myMMSBar, 1)); publishProgress(myMMSBar); // mmsesFound+=1; } } catch (NullPointerException npe) { LOG.error("doInBackground :: null pointer exception occured.\n\t", npe); } finally { // Utilities.close(cur); if (cur != null) { cur.close(); } } /*JAVA 1.7 VERSION try (Cursor cur = myContext.getContentResolver().query(Uri.parse(Constants.mmsBaseUri), null, null, null, null)) { while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex("_id")); boolean sent = cur.getInt(cur.getColumnIndex("msg_box")) == MESSAGE_TYPE_SENT; String trid = cur.getString(cur.getColumnIndex("tr_id")); long tstamp = cur.getLong(cur.getColumnIndex("date")); //String subject = cur.getString(cur.getColumnIndex("sub")); String tmpsub = cur.getString(cur.getColumnIndex("sub")); tmpsub = tmpsub == null ? "" : tmpsub; //String subject = new String(tmpsub.getBytes(ISO_8859_1), UTF_8); String subject = tmpsub; //Log.d(TAG, "subject : "+subject); String type = ""; String pathToAttachment = ""; String attachmentName = ""; ArrayList<String> to = new ArrayList<>(); try(Cursor addr = myContext.getContentResolver().query(Uri.parse(Constants.mmsBaseUri + id + "/addr"), null, null, null, null)){ while(addr.moveToNext()){ String num = addr.getString(addr.getColumnIndex("address")); if(Utilities.isMMSAddressValid(num)) { to.add(num); } } }catch(NullPointerException npe){ LOG.error("doInBackground :: null pointer exception on addr.\n\t", npe); } try(Cursor part = myContext.getContentResolver().query(Uri.parse(Constants.mmsBaseUri+id+"/part"),null,null,null,null)){ while(part.moveToNext()){ String _id = part.getString(part.getColumnIndex("_id")); byte[] fileBytes = Utilities.getMMSPart(myContext, _id); if( fileBytes != null ){ LOG.debug("doInBackground :: mms part found."); type = part.getString(part.getColumnIndex("ct")); //pathToAttachment = part.getString(part.getColumnIndex("_data")); attachmentName = part.getString(part.getColumnIndex("name")); //File workingFolder = new File(myDir); //TODO CREATE FOLDER TREE ONLY IF PART IS NOT NULL, CHECK GETMMSPART RESULT FIRST File relative = new File(trid,attachmentName); //Log.d(TAG, "relative path : "+relative.getPath()); File absolute = new File(myDir, relative.getPath()); new File(myDir, trid).mkdirs(); */ /*File folder = new File(myDir, trid); folder.mkdirs();*/ /* //absolute.mkdir(); //Log.d(TAG, "path : "+absolute.getPath()); try (FileOutputStream fos = new FileOutputStream(absolute)) { fos.write(fileBytes); } catch (FileNotFoundException fne) { //Log.d(TAG, "doInBackground :: FileNotFoundException :: "+fne.getMessage()); LOG.debug("doInBackground :: FileNotFoundException.\n\t", fne); } catch (IOException ioe) { //Log.d(TAG, "doInBackground :: IOException :: "+ioe.getMessage()); LOG.debug("doInBackground :: IOException.\n\t", ioe); } pathToAttachment = relative.getPath(); }else{ LOG.debug("doInBackground :: no mms part found"); } } }catch(NullPointerException npe){ LOG.error("doInBackground :: null pointer exception on part.\n\t", npe); } MMS mms = new MMS(trid, tstamp, to.toArray(new String[to.size()]), pathToAttachment, attachmentName, subject, type, sent); //if(mms.isValid()) { myMMSes.add(mms); //Log.d(TAG, "cursor : \n" + cur.toString()); //publishProgress(new ProgressUpdate(myMMSBar, addedMMSes += 1)); addedMMSes+=1; //} publishProgress(new ProgressUpdate(myMMSBar, 1)); //mmsesFound+=1; } } catch (NullPointerException npe) { LOG.error("doInBackground :: null pointer exception occured.\n\t", npe); //Log.d(TAG,"doInBackground :: null pointer exception occured"); }*/ LOG.debug("doInBackground :: {} mmses found.", addedMMSes); // Log.d(TAG,"doInBackground :: "+ mmsesFound + " corProcessedMMSes found"); Utilities.writeJson(new File(myDir, "mms.json").getPath(), myMMSes); return addedMMSes; }