Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakamiChie/8206551 to your computer and use it in GitHub Desktop.
Save TakamiChie/8206551 to your computer and use it in GitHub Desktop.
File file = /* 適当なFile(外部ストレージ上に保存している画像であること) */
ContentResolver cr = context.getContentResolver();
ContentValues cv = new ContentValues();
cv.put(MediaStore.Images.Media.TITLE, file.getName());
cv.put(MediaStore.Images.Media.DISPLAY_NAME, file.getName());
cv.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
cv.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
cv.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
File file = /* 適当なFile(外部ストレージ上に保存している画像であること) */
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { BaseColumns._ID },
MediaStore.Images.ImageColumns.DATA + " LIKE ?",
new String[] { Uri.fromFile(file).getPath() }, null);
c.moveToFirst();
long id = c.getLong(c.getColumnIndex(BaseColumns._ID));
c.close();
Uri uri = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.toString(id));
File file = /* 送信したいファイルを指定 */
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); /* 追加 */
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); /* 追加 */
intent.setClassName(/*起動したいアプリのパッケージ名*/, /*起動したいアクティビティのクラス名*/);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
try {
startActivityForResult(intent, REQUEST_SEND);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), ”アプリケーションの起動に失敗しました”, Toast.LENGTH_SHORT).show();
}
File file = /* 送信したいファイルを指定 */
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.setClassName(/*起動したいアプリのパッケージ名*/, /*起動したいアクティビティのクラス名*/);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
try {
startActivityForResult(intent, REQUEST_SEND);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), ”アプリケーションの起動に失敗しました”, Toast.LENGTH_SHORT).show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment