레이블이 안드로이드인 게시물을 표시합니다. 모든 게시물 표시
레이블이 안드로이드인 게시물을 표시합니다. 모든 게시물 표시

android setImage failed (사진을 못 가져올 때)

갤러리에서 사진을 선택할 때 사진을 못 가져오는 현상이 발생하면 아래 두 가지 경우를 생각 할 수 있습니다.

1. Permission Denial

java.lang.SecurityException: Permission Denial: reading 
com.android.providers.media.MediaProvider uri 
content://media/external/images/media/38498 from pid=27232, uid=10472 requires 
android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

이 경우는 External storage에서 URI를 가져올 때 권한에 막혀 가져오지 못하는 경우입니다.
위와 같은 경우는 Manifest에 아래와 같이 권한 설정을 해주면 쉽게 가져올 수 있습니다.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


2. 이미지 용량 초과

 삼성 핸드폰의 경우 'Input agif image larger than 30MB.' 라는 로그 메세지를 보실 수 있
습니다. 일정 용량 이상의 사진파일은 불러오지 못하므로, setImageUri(uri)를 아래와 같이
BitmapFactory를 이용해 이미지의 size를 조정해주시면 불러 올 수 있습니다.

uri = data.getData();
try {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    Bitmap myBitmap = BitmapFactory.decodeStream(view.getContext().getContentResolver().openInputStream(uri), null, options);
    selectedImageView.setImageBitmap(myBitmap);
} catch(Exception e){
    e.getStackTrace();
}

Android dynamic change image resource (안드로이드 동적 이미지 변경)

안드로이드 소스코드 내에서의 동적 이미지 변경 방법입니다.

setImageResource(getApplication().getResources().getIdentifier(String name, String defType, String defPackage));

위와같은 방식으로 가져오며, 아래는 소스코드의 예 입니다.

imageView image= (ImageView)findViewById(R.id.test_image);
image.setImageResource(getApplication().getResources().getIdentifier("ic_launcher", "mipmap", "com.testcode.testproject"));

이런 방식으로 소스코드 내에서 이미지를 변경 할 수 있습니다.

Android error: resource entry is already defined

안드로이드에서 error: resource entry name.png is already defined

라는 문구가 뜨면

이미 올라가있던 jpg파일이 등록된 후 png파일이 올라와서 리소스에 중복되어있는 경우입니다.

안드로이드 프로젝트 build -> rebuild project를 하시면 손쉽게 해결 할 수 있습니다.