2022年3月21日 星期一

Android Studio IDE 錯誤

 :app:compile xxxxx JavaWithJavac FAILED

An exception has occurred in the compiler (1.8.0_312). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.

java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)

 at com.sun.tools.javac.util.Assert.error(Assert.java:133)

 at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)

 at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)

 at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)

... 


問題發生原因:

Android Studio Arctic Fox 中更新 gradle jdk 的路徑有問題

Arctic Fox 會使 JDK 1.8 無法正常工作


解決辦法:

更新 Android Studio 

並設定 gradle 路徑

改成 JDK 11 點擊 OK 即可生效。


Windows:

File> Project structure> SDK Location>Gradle Settings


Mac:

Android Studio > Preferences > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK 

2022年3月15日 星期二

Android 分享功能

 

網路圖片
https://stickershop.line-scdn.net/stickershop/v1/sticker/438329029/android/sticker.png

Intent intent = new Intent(Intent.ACTION_SEND);
if (imgPath == null || imgPath.equals("")) {
intent.setType("text/plain"); // 純文字
} else {
File f = new File(imgPath);
if (f != null && f.exists() && f.isFile()) {
intent.setType("image/jpg");
Uri u = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(Intent.createChooser(intent, activityTitle));

Android 網路圖片轉Drawable

1.有一張網路圖片路徑
private void initAudio(){
new DownloadImageTask().execute(YouTubeUtil.getYoutubePic(youtubeId));
}
2. 透過異步下載
private class DownloadImageTask extends AsyncTask<String, Void, Drawable> {
protected Drawable doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}

protected void onPostExecute(Drawable result) {
tracks.add(new Track("Track 0", "Artist 0", result));
// mImageView.setImageDrawable(result);
}
}

Android Drawable 轉 Bitmap

 


BitmapDrawable bd = (BitmapDrawable) track.getDrawable();
icon = bd.getBitmap();

LeetCode 167. Two Sum II - Input Array Is Sorted

 https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/


題目是Input是一個陣列,且經過小到大的排序,需要將兩數相加,回傳該兩數的位置+1,且兩個位置不能相同。

Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].

Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].


解題:
一開始用最直觀的暴力解

LeetCode 9. Palindrome Number

 https://leetcode.com/problems/palindrome-number/


給定一個數字,假如數字從頭到中間和從中間到末端是一樣的
回傳 true,反之回傳 false
範例:
Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

進階解題,建議不要用字串型別來解此題


解題答案

我先直接判斷是否為 0 , 是否為負數, 判斷是否為偶數長度, 是否為奇數長度

Android Studio IDE 錯誤

 :app:compile xxxxx JavaWithJavac FAILED An exception has occurred in the compiler (1.8.0_312). Please file a bug against the Java compiler ...