-
카테고리
-
세부 분야
모바일 앱 개발
-
해결 여부
미해결
갤러리에서 이미지 가져오기가 안됩니다.
21.06.14 13:12 작성 조회수 287
0
강사님 갤러리에서 이미지 가져오기가 안됩니다.
권한등록도 하고 소스코드 강사님이 하신거랑 똑같이 복사 붙여넣기 했는데도
이미지를 가져오지 못하고 있습니다.
확인 부탁드립니다.
package kr.co.softcampus.gallerybasic;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
String [] permission_list = {
Manifest.permission.READ_EXTERNAL_STORAGE
};
ImageView image1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView)findViewById(R.id.imageView);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(permission_list, 0);
}
}
public void getImageBtn(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
// 사진을 선택하고 왔을 때만 처리한다.
if(resultCode == RESULT_OK){
// 선택한 이미지를 지칭하는 Uri 객체를 얻어온다.
Uri uri = data.getData();
// Uri 객체를 통해서 컨텐츠 프로바이더를 통해 이미지의 정보를 가져온다.
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.moveToNext();
// 사용자가 선택한 이미지의 경로 데이터를 가져온다.
int index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
String source = cursor.getString(index);
// 경로 데이터를 통해서 이미지 객체를 생성한다
Bitmap bitmap = BitmapFactory.decodeFile(source);
// 이미지의 크기를 조정한다.
Bitmap bitmap2 = resizeBitmap(1024, bitmap);
// 회전 각도 값을 가져온다.
float degree = getDegree(source);
Bitmap bitmap3 = rotateBitmap(bitmap2, degree);
image1.setImageBitmap(bitmap3);
}
}catch(Exception e){
e.printStackTrace();
}
}
public Bitmap resizeBitmap(int targetWith, Bitmap source){
double ratio = (double)targetWith / (double)source.getWidth();
int targetHeight = (int)(source.getHeight() * ratio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWith, targetHeight, false);
if(result != source){
source.recycle();
}
return result;
}
public float getDegree(String source){
try{
ExifInterface exif = new ExifInterface(source);
int degree = 0;
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
switch (ori){
case ExifInterface.ORIENTATION_ROTATE_90 :
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180 :
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270 :
degree = 270;
break;
}
return (float)degree;
}catch(Exception e){
e.printStackTrace();
}
return 0.0f;
}
public Bitmap rotateBitmap(Bitmap bitmap, float degree){
try{
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
bitmap.recycle();
return bitmap2;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.softcampus.gallerybasic">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GalleryBasic">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
답변을 작성해보세요.
0
답변 1