最新Android开发实用代码片段(二)(1),2024年最新安卓工程师的面试题怎么做

最后

**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。


网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


public static String txt2String(File file){

        String result = "";

        try{

            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件

            String s = null;

            while((s = br.readLine())!=null){//使用readLine方法,一次读一行

                result = result + "\n" +s;

            }

            br.close();

        }catch(Exception e){

            e.printStackTrace();

        }

        return result;

    }

(2)String写入到txt


  /**

     * String写入文件

     * @param fileName

     * @param content

     * @return

     */

    public static boolean writeToTxt(String fileName, String content)

    {

        try

        {

            File newFile = new File(fileName);

            if (newFile.exists())

            {

                newFile.delete();

            }

            int iLen = Util.getLengthString(content);

            OutputStreamWriter write = null;

            BufferedWriter out = null;

            if (!TextUtils.isEmpty(fileName))

            {

                try

                {

                    // new FileOutputStream(fileName, true) 第二个参数表示追加写入

                    write = new OutputStreamWriter(new FileOutputStream(

                            fileName),Charset.forName("gbk"));//一定要使用gbk格式

                    out = new BufferedWriter(write, iLen);

                }

                catch (Exception e)

                {

                }

            }

            out.write(content);

            out.flush();

            out.close();

            return true;

        }

        catch (Exception e)

        {

            return false;

        }

    }

4.drawable转Bitmap


private Bitmap drawableToBitmap(Drawable drawable) {

        int dw = drawable.getIntrinsicWidth();

        int dh = drawable.getIntrinsicHeight();

        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

                : Bitmap.Config.RGB_565;

        Bitmap bitmap=Bitmap.createBitmap(dw, dh, config);

        Canvas canvas = new Canvas(bitmap);

        drawable.setBounds(0, 0, dw, dh);

        drawable.draw(canvas);

        return bitmap;

    }

5.调用系统拨号

有2种方式

第一种:没有拨号界面,直接拨号


  Uri uri=Uri.parse("tel:"+"13012345678");

  Intent intent=new Intent();

  intent.setAction(Intent.ACTION_CALL);//  ACTION_CALL直接拨出

  intent.setData(uri);

  startActivity(intent);

第二种:拨号界面了,号码也输入好了,但是要手动点击拨号


 Uri uri=Uri.parse("tel:"+"13012345678");

 Intent intent=new Intent();

 intent.setAction(Intent.ACTION_DIAL);

 intent.setData(uri);

 startActivity(intent);

当然,不要忘了要加上权限


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

6.调用系统短信界面

第一种,设定发送的号码,和内容,界面没有联系人,群组组等按钮


                Uri smsToUri = Uri.parse("smsto:13012345678");// 联系人地址

                Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,

                        smsToUri);

                mIntent.putExtra("sms_body", "短信内容test123");// 短信内容

                startActivity(mIntent);

效果如下:

**

**

第二种,设定发送短信内容,不设置发送的号码,界面有联系人,群组等按钮


   Uri smsUri = Uri.parse("smsto:");

   Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);

   intent.putExtra("sms_body", "短信内容");

   intent.setType("vnd.android-dir/mms-sms");

   startActivity(intent);

7.调用系统浏览器浏览网页


Uri uri = Uri.parse("http://www.baidu.com");   

 

Intent it   = new Intent(Intent.ACTION_VIEW,uri);   

 

startActivity(it);

8.FileUtil工具类

主要包括:在指定的位置创建指定的文件;在指定的位置创建文件夹;删除指定的文件;删除指定的文件夹;复制文件/文件夹;移动指定的文件(夹)到目标文件(夹)。


/**

 * 文件操作工具类

 */

public class FileUtil {

    /**

     * 在指定的位置创建指定的文件

     *

     * @param filePath 完整的文件路径

     * @param mkdir 是否创建相关的文件夹

     * @throws Exception

     */

    public static void mkFile(String filePath, boolean mkdir) throws Exception {

        File file = new File(filePath);

        file.getParentFile().mkdirs();

        file.createNewFile();

        file = null;

    }

 

    /**

     * 在指定的位置创建文件夹

     *

     * @param dirPath 文件夹路径

     * @return 若创建成功,则返回True;反之,则返回False

     */

    public static boolean mkDir(String dirPath) {

        return new File(dirPath).mkdirs();

    }

 

    /**

     * 删除指定的文件

     *

     * @param filePath 文件路径

     *

     * @return 若删除成功,则返回True;反之,则返回False

     *

     */

    public static boolean delFile(String filePath) {

        return new File(filePath).delete();

    }

 

    /**

     * 删除指定的文件夹

     *

     * @param dirPath 文件夹路径

     * @param delFile 文件夹中是否包含文件

     * @return 若删除成功,则返回True;反之,则返回False

     *

     */

    public static boolean delDir(String dirPath, boolean delFile) {

        if (delFile) {

            File file = new File(dirPath);

            if (file.isFile()) {

                return file.delete();

            } else if (file.isDirectory()) {

                if (file.listFiles().length == 0) {

                    return file.delete();

                } else {

                    int zfiles = file.listFiles().length;

                    File[] delfile = file.listFiles();

                    for (int i = 0; i < zfiles; i++) {

                        if (delfile[i].isDirectory()) {

                            delDir(delfile[i].getAbsolutePath(), true);

                        }

                        delfile[i].delete();

                    }

                    return file.delete();

                }

            } else {

                return false;

            }

        } else {

            return new File(dirPath).delete();

        }

    }

 

    /**

     * 复制文件/文件夹 若要进行文件夹复制,请勿将目标文件夹置于源文件夹中

     * @param source 源文件(夹)

     * @param target 目标文件(夹)

     * @param isFolder 若进行文件夹复制,则为True;反之为False

     * @throws Exception

     */

    public static void copy(String source, String target, boolean isFolder)

            throws Exception {

        if (isFolder) {

            (new File(target)).mkdirs();

            File a = new File(source);

            String[] file = a.list();

            File temp = null;

            for (int i = 0; i < file.length; i++) {

                if (source.endsWith(File.separator)) {

                    temp = new File(source + file[i]);

                } else {

                    temp = new File(source + File.separator + file[i]);

                }

                if (temp.isFile()) {

                    FileInputStream input = new FileInputStream(temp);

                    FileOutputStream output = new FileOutputStream(target + "/" + (temp.getName()).toString());

                    byte[] b = new byte[1024];

                    int len;

                    while ((len = input.read(b)) != -1) {

                        output.write(b, 0, len);

                    }

                    output.flush();

                    output.close();

                    input.close();

                }

                if (temp.isDirectory()) {

                    copy(source + "/" + file[i], target + "/" + file[i], true);

                }

            }

        } else {

            int byteread = 0;

            File oldfile = new File(source);

            if (oldfile.exists()) {

                InputStream inStream = new FileInputStream(source);

                File file = new File(target);

                file.getParentFile().mkdirs();

                file.createNewFile();

                FileOutputStream fs = new FileOutputStream(file);

                byte[] buffer = new byte[1024];

                while ((byteread = inStream.read(buffer)) != -1) {

                    fs.write(buffer, 0, byteread);

                }

                inStream.close();

                fs.close();

            }

        }

    }

 

    /**

     * 移动指定的文件(夹)到目标文件(夹)

     * @param source 源文件(夹)

     * @param target 目标文件(夹)

     * @param isFolder 若为文件夹,则为True;反之为False

     * @return

     * @throws Exception

     */

    public static boolean move(String source, String target, boolean isFolder)

            throws Exception {

        copy(source, target, isFolder);

        if (isFolder) {

            return delDir(source, true);

        } else {

            return delFile(source);

        }

    }

}

9.获取sdk版本号


 public static int getAndroidSDKVersion(){

        int version;

        version = Integer.valueOf(android.os.Build.VERSION.SDK);

        return version;

    }

10.存储bitmap到指定路径


  /**

     * bitmap存储

     * @param mBitmap

     * @param strPath

     */

    public static void saveMyBitmap(Bitmap mBitmap, String strPath,boolean isRecycle)

    {

        if(mBitmap!=null&&!mBitmap.isRecycled()&&!TextUtils.isEmpty(strPath))

        {

            File f = new File( strPath );

            if (f.exists())

            {

                f.delete();

            }

            FileOutputStream fOut = null;

            try {

                fOut = new FileOutputStream(f);

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            }

            mBitmap.compress(Bitmap.CompressFormat.JPEG, 60, fOut);

            try {

                fOut.flush();

            } catch (IOException e) {

                e.printStackTrace();

            }catch (NullPointerException e)

            {

                e.getMessage();

            }

            try {

                fOut.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

            if(!mBitmap.isRecycled()&&isRecycle)

            {

                mBitmap.recycle();

                System.gc();

            }

        }

    }

11.根据原图绘制圆形头像


/**

     * 根据原图和宽度保存圆形图片

     *

     * @param strPath

     * @return

     */

    public static Bitmap SaveCircleImage(String strPath)

    {

        Bitmap source = null;

        try

        {

            source=Util.getLocalBitmap(strPath,true);



            if(source != null)

            {

                final Paint paint = new Paint();

                paint.setAntiAlias(true);

                int iWidth = source.getWidth();

                int iHeight = source.getHeight();



                float fRate = 1.0f;

                if (iWidth > 800 || iHeight > 800)

                {

                    int iSetWidth = iWidth;

                    int iSetHeight = iHeight;

                    while(iSetWidth > 800 || iSetHeight > 800)

                    {

                        fRate = fRate - 0.01f;

                        iSetWidth = (int)(iWidth * fRate);

                        iSetHeight = (int)(iHeight * fRate);

                    }

                }

                iWidth = (int)(iWidth*fRate);

                iHeight = (int)(iHeight*fRate);



                Matrix matrix = new Matrix();

                matrix.postScale(fRate,fRate); //长和宽缩小的比例

                Bitmap resizeBmp = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);



                int min = iWidth;

                if (iHeight < iWidth)

                {

                    min = iHeight;

                }



                Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_4444);



                /**

                 * 产生一个同样大小的画布

                 */

                Canvas canvas = new Canvas(target);

                /**

                 * 首先绘制圆形

                 */

                canvas.drawCircle(min / 2, min / 2, min / 2, paint);

                /**

                 * 使用SRC_IN

                 */

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

                /**

                 * 绘制图片

                 */


## 结尾

**如何才能让我们在面试中对答如流呢?**

答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?为此我整理了一份Android学习资料路线:

![](https://img-blog.csdnimg.cn/img_convert/5a271386d0c959764f75483d940b7ef6.webp?x-oss-process=image/format,png)

这里是一份BAT大厂面试资料专题包:

![](https://img-blog.csdnimg.cn/img_convert/50367d182516773b553ee80d81cb2e47.webp?x-oss-process=image/format,png)

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

      canvas.drawCircle(min / 2, min / 2, min / 2, paint);

                /**

                 * 使用SRC_IN

                 */

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

                /**

                 * 绘制图片

                 */


## 结尾

**如何才能让我们在面试中对答如流呢?**

答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?为此我整理了一份Android学习资料路线:

[外链图片转存中...(img-Vo1ZSLoQ-1715368054024)]

这里是一份BAT大厂面试资料专题包:

[外链图片转存中...(img-3fzOuTHR-1715368054024)]

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值