Created
October 20, 2015 02:30
-
-
Save toybeans/225796f54369c13f70d6 to your computer and use it in GitHub Desktop.
画像とテキストをTwitterとか共有出来るアプリに受け渡す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getShareImageFilePath() { | |
String fname = "sample.jpg"; | |
// ファイル保存先 (SDカード) | |
String fileFullPath = Environment.getExternalStorageDirectory() | |
+ File.separator + fname; | |
return fileFullPath; | |
} | |
/** | |
* SDカードにbitmapを保存する | |
*/ | |
public static void writeBitmapExtarnalStorage(Context context, Bitmap b) { | |
// ファイル保存先 (SD) | |
String fileFullPath = Utility.getShareImageFilePath(); | |
File file = new File(fileFullPath); | |
file.getParentFile().mkdir(); | |
FileOutputStream out = null; | |
try { | |
// 画像をバイト配列に変換 | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
b.compress(Bitmap.CompressFormat.JPEG, 100, os); // PNGの場合は CompressFormat.PNG | |
os.flush(); | |
byte[] w = os.toByteArray(); | |
os.close(); | |
// バイト配列をファイルとしてSDカードに書き出す | |
out = new FileOutputStream(fileFullPath); | |
out.write(w, 0, w.length); | |
out.flush(); | |
} catch (FileNotFoundException ex) { | |
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); | |
} catch (IOException ex) { | |
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); | |
} finally { | |
try { | |
if (out != null) { | |
out.close(); | |
} | |
} catch (IOException ex) { | |
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment