카카오톡과 같은 SNS나 메일 등으로 안드로이드 앱을 공유하고자 할 때가 있는데, 카카오 개발자 등록과 같은 추가적인 절차 없이 코드 몇 줄 만으로도 정말 쉽게 만들 수 있습니다. 함께 빠르게 만들어보도록 해요.
1. 버튼 만들기
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 공유하기 기능 구현하기
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// activity_main의 버튼을 가져오기
Button shareButton = findViewById(R.id.shareButton);
// 클릭 리스너 구현
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
Intent shareIntent = Intent.createChooser(intent, "share");
startActivity(shareIntent);
}
});
}
}
3. 공유할 내용 추가하기
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// activity_main의 버튼을 가져오기
Button shareButton = findViewById(R.id.shareButton);
// 클릭 리스너 구현
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// String으로 받아서 넣기
String sendMessage = "이렇게 스트링으로 만들어서 넣어주면 됩니다.";
intent.putExtra(Intent.EXTRA_TEXT, sendMessage);
Intent shareIntent = Intent.createChooser(intent, "share");
startActivity(shareIntent);
}
});
}
}
4. MIME TYPE이란?
MIME-Type | Description | File Extension |
application/acad | AutoCAD drawing files | dwg |
application/clariscad | ClarisCAD files | ccad |
application/dxf | DXF (AutoCAD) | dxf |
application/msaccess | Microsoft Access file | mdb |
application/msword | Microsoft Word file | doc |
application/octet-stream | Uninterpreted binary | bin |
application/pdf | PDF (Adobe Acrobat) | |
application/postscript | Postscript, encapsulated Postscript, | ai, ps, eps |
Adobe Illustrator | ||
application/rtf | Rich Text Format file | rtf rtf |
application/vnd.ms-excel | Microsoft Excel file | xls |
application/vnd.ms-powerpoint | Microsoft PowerPoint file | ppt |
application/x-cdf | Channel Definition Format file | cdf |
application/x-csh | C-shell script | csh csh |
application/x-dvi | TeX | dvi dvi dvi |
application/x-javascript | Javascript source file | js |
application/x-latex | LaTeX source file | latex |
application/x-mif | FrameMaker MIF format | mif |
application/x-msexcel | Microsoft Excel file | xls |
application/x-mspowerpoint | Microsoft PowerPoint file | ppt |
application/x-tcl | TCL script | tcl |
application/x-tex | TeX source file | tex |
application/x-texinfo | Texinfo (emacs) | texinfo, texi |
application/x-troff | troff file | t, tr, roff t, tr, roff |
application/x-troff-man | troff with MAN macros | man |
application/x-troff-me | troff with ME macros | me |
application/x-troff-ms | troff with MS macros | ms |
application/x-wais-source | WAIS source file | src |
application/zip | ZIP archive | zip |
audio/basic | Basic audio (usually m-law) | au, snd |
audio/x-aiff | AIFF audio | aif, aiff, aifc |
audio/x-wav | Windows WAVE audio | wav |
image/gif | GIF image | gif |
image/ief | Image Exchange Format file | ief |
image/jpeg | JPEG image | jpeg, jpg jpe |
image/tiff | TIFF image | tiff, tif |
image/x-cmu-raster | CMU Raster image | ras |
image/x-portable-anymap | PBM Anymap image format | pnm |
image/x-portable-bitmap | PBM Bitmap image format | pbm |
image/x-portable-graymap | PBM Graymap image format | pgm |
image/x-portable-pixmap | PBM Pixmap image format | ppm |
image/x-rgb | RGB image format | rgb |
image/x-xbitmap | X Bitmap image | xbm |
image/x-xpixmap | X Pixmap image | xpm |
image/x-xwindowdump | X Windows Dump (xwd) | xwd |
multipart/x-gzip | GNU ZIP archive | gzip |
multipart/x-zip | PKZIP archive | zip |
text/css | Cascading style sheet | css |
text/html | HTML file | html, htm |
text/plain | Plain text | txt |
text/richtext | MIME Rich Text | rtx |
text/tab-separated- values | Text with tab-separated values | tsv |
text/xml | XML document | xml |
text/x-setext | Struct-Enhanced text | etx |
text/xsl | XSL style sheet | xsl |
video/mpeg | MPEG video | mpeg, mpg, mpe |
video/quicktime | QuickTime video | qt, mov |
video/x-msvideo | Microsoft Windows video | avi |
video/x-sgi-movie | SGI movie player format | movie |
MIME 확장명 | 파일 확장명 |
application/x-silverlight-app | .xap |
application/manifest | .manifest |
application/x-ms-application | .application |
application/x-ms-xbap | .xbap |
application/octet-stream | .deploy |
application/vnd.ms-xpsdocument | .xps |
application/xaml+xml | .xaml |
application/vnd.ms-cab-compressed | .cab |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docm |
application/vnd.openxmlformats-officedocument.presentationml.presentation | .pptx |
application/vnd.openxmlformats-officedocument.presentationml.presentation | .pptm |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsm |
application/msaccess | .accdb |
application/x-mspublisher | .pub |
image/svg+xml | .svg |
application/xhtml+xml | .xht |
application/xhtml+xml | .xhtml |
댓글
댓글 쓰기