/** * This method creates a cached file exactly copying from the input stream. * * @param sourceUrl the source file path or uri string * @param localFilePath the cache file path string * @param mimeType the mimeType of the source inputstream * @return null if failed, otherwise a StoredFile object */ public static StoredFile createLocalStoredFile(String sourceUrl, String localFilePath, String mimeType) { InputStream is = null; try { Context context = ApptentiveInternal.getInstance().getApplicationContext(); if (URLUtil.isContentUrl(sourceUrl) && context != null) { Uri uri = Uri.parse(sourceUrl); is = context.getContentResolver().openInputStream(uri); } else { File file = new File(sourceUrl); is = new FileInputStream(file); } return createLocalStoredFile(is, sourceUrl, localFilePath, mimeType); } catch (FileNotFoundException e) { return null; } finally { ensureClosed(is); } }
localFilePath += "." + extension; StoredFile storedFile = Util.createLocalStoredFile(uri, localFilePath, mimeType); if (storedFile == null) { return false; // TODO: add error message
@Override protected boolean execute(Conversation conversation) { if (is == null) { return false; // TODO: add error message } CompoundMessage message = new CompoundMessage(); // No body, just attachment message.setBody(null); message.setRead(true); message.setHidden(true); message.setSenderId(conversation.getPerson().getId()); ArrayList<StoredFile> attachmentStoredFiles = new ArrayList<StoredFile>(); String localFilePath = Util.generateCacheFilePathFromNonceOrPrefix(ApptentiveInternal.getInstance().getApplicationContext(), message.getNonce(), null); String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); if (!TextUtils.isEmpty(extension)) { localFilePath += "." + extension; } // When created from InputStream, there is no source file uri or path, thus just use the cache file path StoredFile storedFile = Util.createLocalStoredFile(is, localFilePath, localFilePath, mimeType); if (storedFile == null) { return false; // TODO: add error message } storedFile.setId(message.getNonce()); attachmentStoredFiles.add(storedFile); message.setAssociatedFiles(attachmentStoredFiles); conversation.getMessageManager().sendMessage(message); return true; } }, "add unread message listener");