one simple example in https://stackoverflow.com/questions/10424997/android-how-to-rename-a-file
Tuesday, March 31, 2020
Sunday, March 29, 2020
Common Mistake with Images in Android
content taken from https://www.toptal.com/android/top-10-most-common-android-development-mistakes
Not understanding Bitmaps
Users love content! Especially when the content is well formatted and looks nice. Images, for instance, are extremely nice content, mainly due to their property of conveying a thousand words per image. They also consume a lot of memory. A lot of memory!
Before an image is displayed on the screen, it has to be loaded into the memory. Since bitmaps are the most common way to do this, we’re going to provide an Android programming guide for the whole process:
Let’s say you want to display an image on your screen that you just took with your camera. The total memory needed for this is calculated with the following formula:
memory_needed_in_bytes = 4 * image_width * image_height;
Why 4? Well, the most common / recommended bitmap configuration is
ARGB_8888
. That means that for each pixel we draw, we need to keep 8 bits (1 byte) for the alpha, the red, the greed and the blue channel in memory, in order to properly display it. There are alternatives, like the RGB_565
configuration that requires half the memory than ARGB_8888
, but loses the transparency and the color precision (while maybe adding a green tint).
Let’s assume you have a brand new device with full HD screen and 12 MP camera. The picture you just took is 4000x3000 pixels large and the total memory needed to display it is:
4 bytes * 4000 * 3000 = 48 MB
48 megabytes of your RAM just for a single image!? That’s a lot!
Now let’s take the screen resolution into consideration. You are trying to show a 4000x3000 image on a screen that has 1920x1080 pixels, in worst case scenario (displaying the image full screen) you shouldn’t allocate more than
4 * 1920 * 1080 = 8.3 MB
of memory.
Always follow the Android programming tips for displaying bitmaps efficiently:
- Scale / crop the large image accordingly : - This action resolve many issues when are you dealing with images in android.
- Issue No 1 - Imageview is not displaying images due to high resolution or larger dimensions images. Like Image of Resolution 4608*2592 is not displaying in my imageview app.
- Issue No 2 - Gridview with images taking too much space in memory therefor App crashes due to out of memory.
- Issue No 3 - Taking reasonable time during saving high resolution or larger dimensions images.
- Issue No 4 - Taking reasonable time during displaying images.
Solution:
compress the image like whatsapp https://stackoverflow.com/a/34367979
https://gist.github.com/vipulasri/0cd97d012934531f1266#file-imagecompression
https://abdelhady.net/2015/03/28/android-loading-images-super-fast-like-whatsapp-part-2/
Note:- bitmapImage.compress(Bitmap.CompressFormat.JPEG, 85, output);
dont use PNG in compressformat. PNG which is lossless, will ignore the quality setting. PNG have 0*0 resolution. You cannot compress if your image source is png.
create thumbnail of image via inbuilt class ThumbnailUtils like
compress the image like whatsapp https://stackoverflow.com/a/34367979
https://gist.github.com/vipulasri/0cd97d012934531f1266#file-imagecompression
https://abdelhady.net/2015/03/28/android-loading-images-super-fast-like-whatsapp-part-2/
Note:- bitmapImage.compress(Bitmap.CompressFormat.JPEG, 85, output);
dont use PNG in compressformat. PNG which is lossless, will ignore the quality setting. PNG have 0*0 resolution. You cannot compress if your image source is png.
If you are using PNG format then it will not compress your image because PNG is a lossless format. use JPEG for compressing your image and use 0 instead of 100 in quality.
Quality Accepts 0 - 100
0 = MAX Compression (Least Quality which is suitable for Small images)
100 = Least Compression (MAX Quality which is suitable for Big images)
create thumbnail of image via inbuilt class ThumbnailUtils like
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(myBitmap,300,300);
Friday, March 27, 2020
Display Image to ImageView and Keep Aspect Ratio
Follow this link https://stackoverflow.com/a/25069883
Without using any custom classes or libraries:
<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
scaleType="fitCenter" (default when omitted)
will make it as wide as the parent allows and up/down-scale as needed keeping aspect ratio.
scaleType="centerInside"
if the intrinsic width of src is smaller than parent width will center the image horizontally if the intrinsic width of src is larger than parent width
will make it as wide as the parent allows and down-scale keeping aspect ratio.
ImageView Zoom-in and Zoom-Out
flawless ZOOM (two finger) / ROTATION (two finger) / DRAG (Single finger)
Follow this link https://stackoverflow.com/a/47733482
Follow this link https://stackoverflow.com/a/47733482
Thursday, March 26, 2020
Imp Android Topic
Horizontally Gallery https://www.journaldev.com/9546/android-gallery-view-example-tutorial
Image with Share Icon option https://stackoverflow.com/a/44628655
Image with Share Icon option https://stackoverflow.com/a/44628655
Android Camera Intent: how to get full sized photo?
https://stackoverflow.com/a/6449092
Send a notification when the app is closed
https://stackoverflow.com/questions/39674850/send-a-notification-when-the-app-is-closed
Send a notification when the app is closed
https://stackoverflow.com/questions/39674850/send-a-notification-when-the-app-is-closed
Wednesday, March 25, 2020
How to build a SearchView in a action bar
Follow this link https://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar
Another link https://stackoverflow.com/questions/34825104/add-search-icon-on-action-bar-android
Action Bar Tutorial link https://www.vogella.com/tutorials/AndroidActionBar/article.html
Task 1: only SearchView in action bar
---------------------------------------------------------
1) create res/menu/menu_search.xml
Another link https://stackoverflow.com/questions/34825104/add-search-icon-on-action-bar-android
Action Bar Tutorial link https://www.vogella.com/tutorials/AndroidActionBar/article.html
Task 1: only SearchView in action bar
---------------------------------------------------------
1) create res/menu/menu_search.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:id="@+id/action_search"
android:title="Search"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.SearchView" /> </menu>
2) MainActivity.java
private Menu menu;
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); this.menu = menu; SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView(); search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Overridepublic boolean onQueryTextSubmit(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show();return true; } @Overridepublic boolean onQueryTextChange(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show(); return true; } }); return true; }Task 2: SearchView and Add Photo icon in action bar
---------------------------------------------------------------------------
1) create res/menu/menu_search.xml<menu xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@+id/action_search"android:title="search"app:showAsAction="ifRoom"app:actionViewClass="android.widget.SearchView" /> <itemandroid:id="@+id/action_add_photo"android:title="add photo"android:icon="@drawable/ic_action_add_photo"app:showAsAction="always"> </item></menu>
2) MainActivity.java
private Menu menu;public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); this.menu = menu; SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView(); search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Overridepublic boolean onQueryTextSubmit(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show();return true; } @Overridepublic boolean onQueryTextChange(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show(); return true; } }); return true; }@Override
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: //Toast.makeText(this, "search selected", Toast.LENGTH_SHORT).show(); break; case R.id.action_add_photo: //Toast.makeText(this, "add photo selected", Toast.LENGTH_SHORT).show(); break; default: break; } return true; }
Subscribe to:
Posts (Atom)