728x90
반응형
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<Button
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:id="@+id/previous"
android:backgroundTint="#FF9800"
android:text="FirstImage"
android:onClick="displayFirstImage"></Button>
<Button
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:id="@+id/next"
android:backgroundTint="#009688"
android:text="LastImage"
android:onClick="displayLastImage"></Button>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/picture"
android:layout_marginBottom="10dp"></ImageView>
</LinearLayout>
package kr.co.usingmediastore;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private Cursor cursor;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
imageView = findViewById(R.id.picture);
/*
Android 10부터 "제한된 저장소(Scoped Storage)" 개념이 적용되기 시작함.
예전엔 미디어파일(이미지,동영상,오디오파일들) 사용자가 직접 SDCard(외부 저장소)에
저장가능했으나,
이젠 MediaStore를 통해 읽거나 쓰도록 권장함
*/
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
public void displayFirstImage(View v) {
try {
String[] projection = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
//컨텐트 제공자를 통해서 이미지를 읽음
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,null,null,null);
int size = cursor.getCount();
if (size == 0) {
Toast.makeText(getApplicationContext(),
"장치에 이미가 없음!", Toast.LENGTH_SHORT).show();
}
else {
//첫 번째 이미지를 읽어 화면에 표시함
if (cursor.moveToFirst()) {
String imageLocation = cursor.getString(1);
Toast.makeText(getApplicationContext(), imageLocation, Toast.LENGTH_LONG).show();
File imageFile = new File(imageLocation);
if (imageFile.exists()) {
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
imageView.setImageBitmap(bm);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void displayLastImage(View v) {
try {
String[] projection = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.MIME_TYPE
};
//컨텐트 제공자를 통해서 이미지를 읽음
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,null,null,null);
int size = cursor.getCount();
if (size == 0) {
Toast.makeText(getApplicationContext(),
"장치에 이미가 없음!", Toast.LENGTH_SHORT).show();
}
else {
//마지막 번째 이미지를 읽어 화면에 표시함
if (cursor.moveToLast()) {
String imageLocation = cursor.getString(1);
Toast.makeText(getApplicationContext(), imageLocation, Toast.LENGTH_LONG).show();
File imageFile = new File(imageLocation);
if (imageFile.exists()) {
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
imageView.setImageBitmap(bm);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
728x90
반응형
'App > Android Java' 카테고리의 다른 글
Android_Java(35) - BatteryStatus (0) | 2022.02.23 |
---|---|
Android_Java(34) - ServiceLifeCycle (0) | 2022.02.23 |
Android_Java(33) - MusicService (0) | 2022.02.23 |
Android_Java(32) - ImageDownload (0) | 2022.02.15 |
Android_Java(31) - AsyncTask (0) | 2022.02.15 |
댓글