레이블이 Android Studio인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Android Studio인 게시물을 표시합니다. 모든 게시물 표시

2022년 8월 27일 토요일

Android 절전모드 사용 체크

 Android 폰을 절전모드를 기본을 사용하는 폰을 위해 체크 하는 코드를 가이드 합니다.

절전모드를 쓰는 폰들은 앱이 백그라운드로 내려갈때 일부 자원(네트워크 등)이 바로 다시 회수 되지 않기 떄문에

문제가 발생할 소지가 있습니다.

​

그에 따른 적절한 조치를 하기 위해서는 해당 모드를 쓰고 있는지 체크 하고 그것에 따른 예외 처리를 추가 해야 합니다.


PowerManager oPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (oPM != null)
{
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && oPM.isPowerSaveMode())
	{
		//절전모드 사용.
	}
}

그럼 ^^

Android 갤러리 이미지 가져오기

 Android 앱 개발시 자신의 갤러리 이미지를 가져오는 기능을 개발하는 경우가 있습니다.

​

Android O/S 버젼이 올라가면서 접근 제한이 많이 일어나고 갤러리 프로그램도 로컬 방식이 아닌

클라우드 방식(구글포토) 을 쓰는것들도 있기 때문에 그것들에 대한 처리 가이드를 안내하려 합니다.

코드는 Java로 구성 되어 있습니다.

​

일단 Android 주 앱 소스에 AndroidManifest.xml 에 권한을 추가 합니다.

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

갤러리 호출은 Intent 를 통해서 호출하여 가져 올수 있습니다.

아래 코드를 Activity Content 를 통해서 호출 합니다.

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 ((Activity)m_oContext).startActivityForResult(i, 1);

startActivityForResult 호출시 두번째 인자는 호출후 돌려받는 리턴 코드값입니다.

편의상 1 로 설정 했습니다.

​

갤러리 호출후 리턴받는 곳은 활성화된 Activity 에서 돌려 받아 처리 합니다.

코드는 아래와 같습니다.


@Override
protected void onActivityResult(int nReqCode, int nRetCode, @Nullable Intent data)
{
    super.onActivityResult(nReqCode, nRetCode, data);

    switch (nReqCode)
    {
        case 1://Gallery
        {
            if (nRetCode == RESULT_OK && data != null)
            {
                Bitmap bmImage = null;
                try
                {
                    Uri oSelectImg = data.getData();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
                    {
                        bmImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), oSelectImg);
                    }
                    else
                    {
                        ImageDecoder.Source oSrc = ImageDecoder.createSource(this.getContentResolver(), oSelectImg);
                        bmImage = ImageDecoder.decodeBitmap(oSrc);
                    }

                    if(m_oTestMainView != null)
                    {
                        m_oTestMainView.m_oImgGallery.setImageBitmap(bmImage);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            else if(nRetCode == RESULT_CANCELED)
            {
                Toast.makeText(this, "사진 선택 취소", Toast.LENGTH_LONG).show();
            }
        }break;
    }
}

"m_oTestMainView" 는 별도로 구성된 LinearLayout 입니다. 그 안에 ImageVIew m_oImgGallery 를 만들어 놓고 불러들인 갤러리 이미지를 비트맵 으로 셋팅하면 이미지가 나옵니다.

​

갤러리 이미지는 직접 Uri 경로로 접근해서 읽어 들일수도 있으나 위에서 언급한것처럼 클라우드 기반 갤러리는 경로를 가져올수 없습니다.

​

그래서 범용적으로 처리를 하시려면 Bitmap 을 자기 앱 경로에 직접 이미지 파일로 저장하여 처리 하는것을 권장합니다.

​

Bitmap 을 이미지 파일로 저장 하시려면

​

저장할 File의 OutputStream 을 만들고 아래 코드를 호출하여 저장하시면 됩니다.


File oFile = new File(context.getFilesDir(), '저장상대경로');
OutputStream osFile = new FileOutputStream(oFile);
bmImage.compress(Bitmap.CompressFormat.PNG, 100, osFile);

그럼 ^^

2020년 9월 16일 수요일

Android Souce Inspect 사용예

 Android 개발을 하다 보면 기준 API를 지정하고 지정된 API 이상 사용되지 않게 작업을 합니다.

하지만 여러 개발자가 같이 작업을 하다 보면 이 기준이 어긋날때가 있기 때문에 이걸 검증하고 수정하는

방법을 이야기 할까 합니다.

​

첫번째로 minsdk 를 지정하고 이것보다 높은 함수를 사용할때 개발을 하고 나면 빌드는 되지만 조건이 충족되지 않는 폰에서는 에러가 발생하며 앱이 크래쉬가 일어 날겁니다.

검증하는 방법은

Android Studio Menu에서 "Analyze" -> "Inspect Code" 를 누릅니다.

누르고 나면 한참동안 작업을 한후 결과창에 Inspect된 내용이 보일겁니다.

위 내용에 해당하는것은 아래 경로로 가시면 확인할수 있습니다.

Android -> Lint -> Correctness -> Calling new methods on older versions

​

두번쨰로는 depercate 된 API를 처리하는 찾아내어 수정하는 방법입니다.

첫번쨰와 마찬가지로 Inspect Code 를 누르시고나서 결과창에

Java -> Code maturity -> Deprecated API usage

위 내용으로 걸러지는 내용을 수정하시면 됩니다.

​

그럼 ~~

2018년 1월 12일 금요일

How to control loading screen (white / black) when running Android App

After you develop and run your app in Android Studio, you'll see an initial white or black screen popping up and running your code.

This is what happens after building with Android Studio. I had never seen this phenomenon when I was building it in Eclipse before (maybe I was not interested.

This phenomenon occurs when the number of jobs in OnCreate increases.

To prevent this, you can do the following three methods.

1. Transparent treatment.
  It is a way to make it disappear altogether. However, if you use this method, the app may seem to stop for a while after you press the button.
 Open the res-> values-> styles.xml file and add the following two items to the style item set in AndroidManifest.xml.
<item name = "android: windowContentOverlay"> @ null </ item>
<item name = "android: windowIsTranslucent"> true </ item>

2. Color change processing.
 How to change the color to the color you want.
 Open the res-> values-> styles.xml file and add the following two items to the style item set in AndroidManifest.xml.
<item name = "android: windowBackground"> ​​@ color / red </ item>

3. Image processing.
How to specify an image in the same way as iPhone.
 Open the res-> values-> styles.xml file and add the following two items to the style item set in AndroidManifest.xml.
<item name = "android: windowBackground"> ​​@ drawable / splash </ item>

 Of course, the above image must be registered.

Then ^^;

2017년 10월 21일 토요일

Android WIFI Debuging

Today we talk about Android WIFI debugging.
iOS has recently provided WIFI debugging, but Android has been around for a while now.
For debugging, the development PC and smartphone must be in the same network.
How to set it is as follows.
Navigate to the folder where adb is installed and open the Window Command window in that location.
adb -d tcpip 9999.
Then the following message appears.
restarting in TCP mode port: 9999
Next, obtain the local IP of the smartphone to be debugged.(You can easily get information by installing a tool to get WIFI information from Google Play.)
Once you have obtained the IP, type Command as shown below.
After typing adb connect XXX.XXX.XXX.XXX:9999, the following message is displayed and the connection is successful.
connect to XXX.XXX.XXX.XXX:9999
How to verify your connection
If you type adb devices, you can check the list.
List of devices attachedXXX.XXX.XXX.XXX:9999 device
Finally, the way to turn debugging connected with WIFI back to USB is as follows.
adb -s XXX.XXX.XXX.XXX:9999 usb
Feel free to develop and develop wireless ^^

2017년 2월 14일 화요일

Android Studio 에서 "error failed to crunch file max path" 에러시 처리방법.

Android Studio 에서 빌드시 "error failed to crunch file max path" 에러가 나타날때 처리 방법입니다.
맥장비랑 윈도우 장비 모두 사용중인데 유독 윈도우 장비에서만 나타나서 검색을 해보니
빌드되는 패스가 240자가 넘어가면 나타나는 오류라고 하네요. (ㅡㅡ.)
다행이도 처리 방법이 존재 합니다.
프로젝트에 최상위 build.gradle 파일에 아래와 같이 셋팅 하여 처리 하면


allprojects {
    buildDir = "C:/tmp/${rootProject.name}/${project.name}"
    repositories {
       ...
    }
}



해당 패스로 빌드 경로를 설정 하기 때문에 "error failed to crunch file max path" 오류가 나타나지 안습니다.