برنامه نویسی اندروید

اموزش برنامه نویسی اندروید تخصصی

برنامه نویسی اندروید

اموزش برنامه نویسی اندروید تخصصی

۳ مطلب با موضوع «java» ثبت شده است

چرا از switch بجای if استفاده نکنیم؟
بجای اینکه چند تا if یا else تعریف کنیم مینوانیم از دستور switch استفاده کنیم در تصویر تعداد خط ها را مشاهده میکنید
حلقه ای به نام switch تعریف کنید و درون آن مقدار خود را بزارید و یک case تعریف کنید و در ادامه شرط حود را بنویسید و با یک : جهت درست بودن عملکرد خود را بنویسید و یادتون باشه دستور switch به break حساسه و آخر هر case اون رو بنویسید
 

  • vahid hasani

Convert bitmap

vahid hasani | | ۰ نظر

Convert bitmap to Byte array using ByteArrayOutputStream

 

import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; public class Main { public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle) { byte[] result = null; ByteArrayOutputStream output = null; try {/* w w w .ja v a 2s . c om*/ output = new ByteArrayOutputStream(); bitmap.compress(mCompressFormat, 100, output); result = output.toByteArray(); if (needRecycle) { bitmap.recycle(); } } catch (Exception e) { e.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } } return result; } }

  • vahid hasani

 

try {
    // write string to a file
    Files.write(Paths.get("output.txt"), "Hey, there!".getBytes());

} catch (IOException ex) {
    ex.printStackTrace();
}

To specify a different character encoding other than the default UTF-8, you can do the following:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");
    
    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16);

} catch (IOException ex) {
    ex.printStackTrace();
}

To create a non-existing file or append the string to an existing one, use the following code snippet:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");

    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}
  • vahid hasani