Sending Images Between Activities - Android (Kotlin/Java) - java

I have a class which encapsulates the information I want to pass over and it is parcelable. So I can pass its properties from one activity to another by using Intent.putExtra() method. But I cannot send images with it since images' sizes are larger than it should be (it doesn't matter whether they are Bitmap or ByteArray).
User downloads images from Firebase Storage and it is very likely that when user views an image, she won't see it again. So saving images in a cache-like storage isn't really necessary.
In Swift, I can do something like this:
// We are in currentViewController which is the activity that will send the image
let someNewViewController = NewViewController() // Create an instance of NewViewController class which is the activity that will receive image
someNewViewController.image = someImage // NewViewController class has a property called image, assign it an image from currentViewController
navigationController.pushViewController(someNewViewController, animated: True) // Go to someNewViewController activity
And then you can easily use image in the someNewViewController by accessing image property. When someNewViewController deallocated, image will be removed by garbage collection.
Is there a similar way in Kotlin/Java? Or is there an alternative to putExtra() that will allow large files to be transferred?
Thanks in advance!

Related

Searching a method like getResourcesID(int)

I'm searching a method that gives me the ID (int) from the Resource from a ImageView in my App. I don't know the name of the Image or something else. i only have an imageview and there is an Image from my Resources inside.
The reason is that i need it is to save it in my SQLite Database as an Integer.
Because later i will read this Data in my SQLite Database to use the method
ImageView.setImageResource((int) R.drawable.SavedPic).
I'm only a beginner in programming so it will not be the best way to do it but i coding the hole day and i hope that you can give me the right method.
There is no direct way to get the Image Resource Id from an ImageView. But there is a way to hold onto that using tags. So if you have an ImageView called imageView and an image resource called R.drawable.image you can do something like this:
imageView.setImageResource(R.drawable.image);
imageView.setTag(R.drawable.image);
And later when you want to get the resource Id associated with the ImageView you can fetch it using
int imageResourceId = (Integer) imageView.getTag();

android: getBackground.setAlpha() changes alpha of all activities with same background-image

For example, if I add the following line to my 2nd activity, the alpha value for the background image will also change in my 1st activity, because I used the same image.
findViewById(R.id.main).getBackground().setAlpha(100);
But I don't want that. The two activities should have different alpha values, but still have the same drawable as background image. How can I achieve this?
You should call mutate() to create a separate instance before setting the alpha. As per the documentation:
A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

Android loading images to ListView AsyncTask

This is more a question of application logic and performance.
Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.
The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.
This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.
Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?
Everytime I go this route the application crashes...
You can use Picasso library for this task.
Visit http://square.github.io/picasso/
In the adapter of your ListView, in the method getView, you can put this:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) .centerInside()
.into(imgView);
You dont need a AsyncTask because this library already implements itself.

How do I insert multiple image in Edit Text in android

I am trying to insert multiple images in one editText. I have used following code to attach image to EditText
txt = (EditText)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeFile(attach);
Drawable drawable = new BitmapDrawable(bitmap);
txt.setBackgroundDrawable(drawable);
But the problem is it only attach one file and show, If i used array then only the last image is only show. at any how it only shows one Image. Is there any way to show multiple images in one Editbox. Thanks in advance.
Because you can only have one background.... if you want more then there is a layer Drawable which you can use and also you can put the button in a frame layout and add a couple of imageViews below/over it for the rest of the images.
But the best solution will probably be instead of having a couple of bitmaps to just make a single one in Photoshop or equivalent photo editing app and place that one.
According to docs: http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)
You are not able to provide more than one Drawable item in argument
And I don't know if you understand what is .setBackgroundDrawable(d) method purpose, but it is not meant to be used for displaying images inside text but to set Background of EditText View.
So you're not inserting images in EditText but setting its background,
look for some Rich Text Edit components usable for Android
such as http://code.google.com/p/android-richtexteditor/
or others..
You can probably use a LayerDrawable with the constructor LayerDrawable(Drawable[] layers) to chain together several images and display them as an EditText's background, though as Marek said, I suspect you're looking for something other than setting images in the background.

Modifying single list items

I have a ListView, displaying some items, containing an ImageView filled with a standard image at first and a line of text.
What I wanna do is downloading one thumb after another with an AsyncTask and step for step change the standard image with the downloaded one. How to realise this?
The ListView contents are managed by an enhanced ArrayAdapter.
greetz
EnflamedSoul
If I understand you correctly you're interested in a LazyList. This example shows a stub for the image, then downloads some thumbnails as the user scrolls down and caches these onto an SD card. The next time the images will be loaded from the SD card.
Lazy load of images in ListView
I personally used Fedor's example and it worked well.

Categories