use of java.io.FileOutputStream in project Talon-for-Twitter by klinker24.
the class SendTweet method sendTweet.
public boolean sendTweet(AppSettings settings, Context context) {
try {
Twitter twitter = getTwitter();
if (remainingChars < 0 && !pwiccer) {
// twitlonger goes here
TwitLongerHelper helper = new TwitLongerHelper(message, twitter);
helper.setInReplyToStatusId(tweetId);
return helper.createPost() != 0;
} else {
twitter4j.StatusUpdate reply = new twitter4j.StatusUpdate(message);
reply.setInReplyToStatusId(tweetId);
if (!attachedUri.equals("")) {
// context being the Activity pointer
File outputDir = context.getCacheDir();
File f = File.createTempFile("compose", "picture", outputDir);
Bitmap bitmap = getBitmapToSend(Uri.parse(attachedUri), context);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
if (!settings.twitpic) {
reply.setMedia(f);
twitter.updateStatus(reply);
return true;
} else {
TwitPicHelper helper = new TwitPicHelper(twitter, message, f, context);
helper.setInReplyToStatusId(tweetId);
return helper.createPost() != 0;
}
} else {
// no picture
twitter.updateStatus(reply);
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of java.io.FileOutputStream in project Lazy by l123456789jy.
the class FileUtils method saveStrToFile.
/**
* 将字符串以charsetName编码保存到文件中
* @param str 保存的字符串
* @param fileName 文件的名字
* @param charsetName 字符串编码
* @return 是否保存成功
*/
public static boolean saveStrToFile(String str, String fileName, String charsetName) {
if (str == null || "".equals(str)) {
return false;
}
FileOutputStream stream = null;
try {
File file = new File(fileName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
byte[] b = null;
if (charsetName != null && !"".equals(charsetName)) {
b = str.getBytes(charsetName);
} else {
b = str.getBytes();
}
stream = new FileOutputStream(file);
stream.write(b, 0, b.length);
stream.flush();
return true;
} catch (Exception e) {
return false;
} finally {
if (stream != null) {
try {
stream.close();
stream = null;
} catch (Exception e) {
}
}
}
}
use of java.io.FileOutputStream in project Lazy by l123456789jy.
the class IOUtils method saveTextValue.
/**
*保存文本
* @param fileName 文件名字
* @param content 内容
* @param append 是否累加
* @return 是否成功
*/
public static boolean saveTextValue(String fileName, String content, boolean append) {
try {
File textFile = new File(fileName);
if (!append && textFile.exists())
textFile.delete();
FileOutputStream os = new FileOutputStream(textFile);
os.write(content.getBytes("UTF-8"));
os.close();
} catch (Exception ee) {
return false;
}
return true;
}
use of java.io.FileOutputStream in project cordova-android-chromeview by thedracle.
the class CameraLauncher method onActivityResult.
/**
* Called when the camera view exits.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// Get src and dest types from request code
int srcType = (requestCode / 16) - 1;
int destType = (requestCode % 16) - 1;
int rotate = 0;
// If CAMERA
if (srcType == CAMERA) {
// If image available
if (resultCode == Activity.RESULT_OK) {
try {
// Create an ExifHelper to save the exif data that is lost during compression
ExifHelper exif = new ExifHelper();
try {
if (this.encodingType == JPEG) {
exif.createInFile(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/.Pic.jpg");
exif.readExifData();
rotate = exif.getOrientation();
}
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = null;
Uri uri = null;
// If sending base64 image back
if (destType == DATA_URL) {
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
if (bitmap == null) {
// Try to get the bitmap from intent.
bitmap = (Bitmap) intent.getExtras().get("data");
}
// Double-check the bitmap.
if (bitmap == null) {
Log.d(LOG_TAG, "I either have a null image path or bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
if (rotate != 0 && this.correctOrientation) {
bitmap = getRotatedBitmap(rotate, bitmap, exif);
}
this.processPicture(bitmap);
checkForDuplicateImage(DATA_URL);
} else // If sending filename back
if (destType == FILE_URI || destType == NATIVE_URI) {
if (this.saveToPhotoAlbum) {
Uri inputUri = getUriFromMediaStore();
//Just because we have a media URI doesn't mean we have a real file, we need to make it
uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
} else {
uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg"));
}
if (uri == null) {
this.failPicture("Error capturing image - no media storage found.");
}
// If all this is true we shouldn't compress the image.
if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 && !this.correctOrientation) {
writeUncompressedImage(uri);
this.callbackContext.success(uri.toString());
} else {
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
if (rotate != 0 && this.correctOrientation) {
bitmap = getRotatedBitmap(rotate, bitmap, exif);
}
// Add compressed version of captured image to returned media store Uri
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
os.close();
// Restore exif data to file
if (this.encodingType == JPEG) {
String exifPath;
if (this.saveToPhotoAlbum) {
exifPath = FileHelper.getRealPath(uri, this.cordova);
} else {
exifPath = uri.getPath();
}
exif.createOutFile(exifPath);
exif.writeExifData();
}
}
// Send Uri back to JavaScript for viewing image
this.callbackContext.success(uri.toString());
}
this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
bitmap = null;
} catch (IOException e) {
e.printStackTrace();
this.failPicture("Error capturing image.");
}
} else // If cancelled
if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("Camera cancelled.");
} else // If something else
{
this.failPicture("Did not complete!");
}
} else // If retrieving photo from library
if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = intent.getData();
// and there will be no attempt to resize any returned data
if (this.mediaType != PICTURE) {
this.callbackContext.success(uri.toString());
} else {
// rotating, nor compressing needs to be done
if (this.targetHeight == -1 && this.targetWidth == -1 && (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
this.callbackContext.success(uri.toString());
} else {
String uriString = uri.toString();
// Get the path to the image. Makes loading so much easier.
String mimeType = FileHelper.getMimeType(uriString, this.cordova);
// If we don't have a valid image so quit.
if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
Log.d(LOG_TAG, "I either have a null image path or bitmap");
this.failPicture("Unable to retrieve path to picture!");
return;
}
Bitmap bitmap = null;
try {
bitmap = getScaledBitmap(uriString);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null) {
Log.d(LOG_TAG, "I either have a null image path or bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
if (this.correctOrientation) {
rotate = getImageOrientation(uri);
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(rotate);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
}
// If sending base64 image back
if (destType == DATA_URL) {
this.processPicture(bitmap);
} else // If sending filename back
if (destType == FILE_URI || destType == NATIVE_URI) {
// Do we need to scale the returned file
if (this.targetHeight > 0 && this.targetWidth > 0) {
try {
// Create an ExifHelper to save the exif data that is lost during compression
String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg";
// Some content: URIs do not map to file paths (e.g. picasa).
String realPath = FileHelper.getRealPath(uri, this.cordova);
ExifHelper exif = new ExifHelper();
if (realPath != null && this.encodingType == JPEG) {
try {
exif.createInFile(realPath);
exif.readExifData();
rotate = exif.getOrientation();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os = new FileOutputStream(resizePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
os.close();
// Restore exif data to file
if (realPath != null && this.encodingType == JPEG) {
exif.createOutFile(resizePath);
exif.writeExifData();
}
// The resized image is cached by the app in order to get around this and not have to delete you
// application cache I'm adding the current system time to the end of the file url.
this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
this.failPicture("Error retrieving image.");
}
} else {
this.callbackContext.success(uri.toString());
}
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
System.gc();
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("Selection cancelled.");
} else {
this.failPicture("Selection did not complete!");
}
}
}
use of java.io.FileOutputStream in project cordova-android-chromeview by thedracle.
the class FileUtils method write.
/**
* Write contents of file.
*
* @param filename The name of the file.
* @param data The contents of the file.
* @param offset The position to begin writing the file.
* @param isBinary True if the file contents are base64-encoded binary data
* @throws FileNotFoundException, IOException
* @throws NoModificationAllowedException
*/
/**/
public long write(String filename, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
if (filename.startsWith("content://")) {
throw new NoModificationAllowedException("Couldn't write to file given its content URI");
}
filename = FileHelper.getRealPath(filename, cordova);
boolean append = false;
if (offset > 0) {
this.truncateFile(filename, offset);
append = true;
}
byte[] rawData;
if (isBinary) {
rawData = Base64.decode(data, Base64.DEFAULT);
} else {
rawData = data.getBytes();
}
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
FileOutputStream out = new FileOutputStream(filename, append);
byte[] buff = new byte[rawData.length];
in.read(buff, 0, buff.length);
out.write(buff, 0, rawData.length);
out.flush();
out.close();
return rawData.length;
}
Aggregations