Demo
구성
- permission
- activity
- layout
- [AndroidMaifest.xml]
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- [UploadNotice.java]
package com.sjcoding.admincollegeapp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import java.io.IOException;
public class UploadNotice extends AppCompatActivity {
private CardView addImage; // 클릭 버튼
private ImageView noticeImageView; // 가져온 사진 그릴 공간
private final int REQ = 1000; // 응답
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_notice);
noticeImageView = (ImageView) findViewById(R.id.iv_notice);
addImage = findViewById(R.id.add_image);
addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGallery();
}
});
}
// gallery 열기
private void openGallery() {
Intent pickImage = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickImage, REQ);
}
// 결과값 bitmap 변환
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
noticeImageView.setImageBitmap(bitmap);
}
}
}
- [activity_upload_notice.xml]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".UploadNotice"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:id="@+id/add_image"
app:cardElevation="5dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background='@drawable/circle_purple'
android:src="@drawable/ic_notice"
android:padding="15dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lightGray"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image"
android:textStyle="bold"
android:padding="5dp"
android:layout_marginTop="10dp"
android:textColor="@color/textColor"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Notice title"
android:id="@+id/notice_title"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload Notice"
android:layout_marginTop="16dp"
android:textAllCaps="false"
android:id="@+id/btn_upload_notice"/>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="400dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:id="@+id/iv_notice"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</ScrollView>
'System Programming > Android' 카테고리의 다른 글
[Android/Firebase] Firebase Upload pdf (0) | 2021.07.01 |
---|---|
[Android/Firebase] Firebase Database, Storage (0) | 2021.06.30 |
[Android/Java] Fragment Bottom Navigation view (0) | 2021.06.28 |
[Android/Java] Fragment간 값 전달 (0) | 2021.06.28 |
[Android/Java] BlankFragment (0) | 2021.06.28 |