android :activity - android

i have two activities
1) downloading something on oncreate function with help of asyn task ,and had one button.
2) secons activity display on click of button.
now when i go back to previous ie first activity , then downloading starts again, i want to get the previous filled data view ,instead of startg the previous process again ..
canu please guide me
thanks

You should set a default value to whatever it is you're downloading, for example a default image, text or number, and then before downloading again, check whether the stored value is the default one or something new. If it's new, then you don't have to download again.
For example, in my game I have a similar thing. It's a jigsaw puzzle game with many images, where all the images are displayed in a ListView. To save space, I didn't include both the full size image and a thumbnail, but instead generate the thumbnails when the game loads and saved in a Bitmap[] array. So that bitmap generation process is similar to your downloading.
Whenever my game is about to load a list or access the images, it first checks whether the array is null. If it is, then it restarts the loading process. If it isn't, then it can use them. This is done with a simple check in onResume():
if (imageThumbnails == null) {
// Do something to reload the images
} else {
//the images are available, so they can be used
}
You should be able to do something similar for your app.

Related

Handing Drawing / Painting / Saving of multiple bitmaps

Goal:
I want to draw / write / paint on bitmaps from pdf and save them together so I could send them email.
Details:
I have multiple Pdf files containing 5-20 pages each, right now am extracting bitmaps from pdfs and loading them in fragments in ViewPager,
where I can swipe through them and write / draw or whatever I want. To save / restore the current state of the bitmap I'm using onSaveInstanceState.
This way I'm able to retrieve the last stage of the bitmap while swiping back and forth.
Something like this:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("Bitmap", bitmap);
}
Problem:
User presses send email button in and I should be able to save all the images regardless of which page he is on i.e. he has edited the page 5 to 10
and now he is on page 10 and press the send button I should be able to retrieve all the edited images.
Problem 2:
As I have stated the number of images are about 5-20, Saving the state in onSaveInstanceState works great but it keeps increasing the app memory in ram, and the app crashes at 10th image where the app size goes to 150Mbs.
But saving the image as bundle in onSaveInstanceState is also necessary because it helps for faster loading of images when user swipes back n forth.
So I was thinking is there any way we can create a custom bundle
class where we can write the image to disk after say 5 pages scrolled
and clears the memory and read it back from disk to memory when user
is scrolling back? does this sound any reasonable?
What I have tried:
As bitmaps are loaded in seperate fragments I tried to save the bitmaps to external storage in OnDestroy, but as saving bitmaps to disk is not a small task, it takes time though am using AsyncTask
but when user is swiping bit fast between fragments then saving bitmaps in OnDestroy does not seems like a convinient way.
This is why I'm asking the question so maybe you have better understanding of the situation and can help me achieve this in a more convinient way.
So what do you purpose how I can handle this situation?
Note:
if you have trouble understanding the situation and need more info leave me a comment and I will update the question.
Thanks
Let me describe how i am seeing the solution of this issue now.
There should be a Service which takes an edited bitmap with some id (same for all modifications of original bitmap), adds it to queue and then each item of that queue it persists to the external storage. It has limited Executor instance which executes that tasks (them could be just Runnable) and Service creates a HashMap where maps Future of task to bitmap identifier. So when new Bitmap has came, Service checks the map and replaces waiting task (with same original bitmap) if any have existed.
You can send a task to this Service from destroyItem interceptor of FragmentStatePagerAdapter.
So, when user clicks on save button you should also send all edited bitmaps left to this Service and just wait for completion.

Problems with Listpicker App Inventor

I'm a beginner at App Inventor and I don't know what I'm doing wrong with the listpicker.
I am trying to create and app to reproduce the music I have stored in my server but when i display the listpicker I can't click any of the options and also I can't go back to the first screen. Here I put my code:
Image 1
Image 2
I tried to remove the line that says call listpicker.open but it only made appear a totally black screen.
The result of the code I just posted is exactly what I spect a list with the name and the link of the 2 songs I already upload to my server but when I click them it didn't do anything.
Thanks for your help.
The Web component works asynchronously, which means, it takes a little bit, until the result is available. The result you will get in the Web.GotText event.
Therefore it does not make sense to call the updateListpicker procedure in the Listpicker.BeforePicking event, because the result still is not available and you get displayed an empty listpicker. The listpicker will be opened, before you have received the result!
Set the listpicker to visible=false and use a button.click event to call the updateListpicker procedure. Then as you already do it in the Web.GotText event, assign the received list to the listpicker and open it.

startActivity(intent) not working in Google glass

I am developing an app for Google glass using using a Immersion Pattern.
I am using start activity to switch between different tiles using below code.
Intent showImageDetails = new Intent(MainActivity.this, CapturedImageActivity.class);
showImageDetails.putExtra("IMAGE_DATA", data);
startActivity(showImageDetails);
data variable holds byte array for captured image.
Few time device is not able to start the activity and it exits the application and goes back to OK Glass tile.
Have anyone observed this issue?
I have used charades as an example which comes with API example.
Based on your comment, my wild guess is your image is too big to be sent with intent. Have you noticed the JAVA BINDER FAILURE error in the log or TransactionTooLargeException:
The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.
Also see Passing data of a non-primitive type between activities in android:
What you do not want to do is pass big stuff via extras. For example, if you are creating an application that grabs pictures off the camera, you do not want to pass those in extras -- use a static data member (icky as that sounds). Intents are designed to work cross-process, which means there is some amount of data copying that goes on, which you want to avoid when it is not necessary for big stuff.
I think if your image is large, it is better to pass the URI or ResourceID of the image but not the image itself. Hope this helps.

When to Load image in ListView?

When I use ListView the getView() method is called many times. Every time when the getView() is called i load the image with Asyc task. I mean every time i reset the image which is annoying.
How to understand when to load the image?
You should cache loaded images, by storing i.e. on SD card, so once you got a copy there, no need to download it again. There's lot of ready-to-use classes that can do the job for you, like:
http://greendroid.cyrilmottier.com/reference/greendroid/widget/AsyncImageView.html
you must must have two flags.
One which says if you've already loaded the image, if true you do nothing.
One which says if you're currently loading the image, if true you do nothing.
The members will also help you on maintaining the state of the image.
Your code should look something like this:
private boolean isLoading = false;
private boolean hasLoaded = false;
if(!hasLoaded){
if(!isLoading){
isLoading = true;
//do async load
//on positive completition callback set hasLoaded to true
//on negative completition callback set isLoading to false
}
}
One of the best solution is to create image cache using the WeakReference. This way you can keep images in memory and only need load from server when they are not in memory. In this method the image would be removed from the memory when system encounter low memory situation. So your current activity would always keep the hard reference to the bitmap's required and the image cache would keep the weak reference to the bitmap's.
below reference links will help you
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
http://www.codeproject.com/Articles/35152/WeakReferences-as-a-Good-Caching-Mechanism
the Volley library (made by google) has a very intuitive class for an imageView that can have a url , called "NetworkImageView" .
you should check it out and watch the video, since they show that it's quite annoying to do it using asyncTask (plus the asyncTask is known to have a limit of tasks, about 255 or so) .
for setting the url, just use setImageUrl .
it has some useful methods for the phases of loading too: setDefaultImageResId , setErrorImageResId.
it's also supposed to have built in caching mechanism of some sort, but i haven't read much about it, so you might want to check out their samples.
this will remove the need to use asyncTasks for the listView's items.
one of my questions regarding the volley includes a sample code , here .
You can add a caching layer and optionally preloading the images. A good strategy for caching Images (Bitmap objects to be exact) is to use a strategy called LRU or least recently used.
Android support library has a class called LruCache that implements this strategy. So for example, when you download/load the image for the first time, you stick it into the cache. later, you can first check if it's already in cache and load it from there.
For preloading, A good rule of thumb is to preload the previous ten and the next ten items.

ListView asynchronous image loading strategy

I currently have a ListView with a custom adapter that gets information describing the content of the rows asynchronously. Part of each row is an image URL, that I'm planning to download asynchronously and then display.
My current plan for a strategy to download these images is:
Keep a cache of soft references to downloaded Bitmap objects.
When a getView() is called and the bitmap is in the cache, set the bitmap for the ImageView directly.
If the bitmap isn't in the cache, start loading it in a separate thread, after the download is complete add it to the cache and call notifyDataSetChanged() on the adapter.
I am also planning to kill pending downloads when the Activity object owning the ListView's onDestroy()-method (Or possibly even in the onPause()-method) is called, but most importantly I want to kill the download of pending images when the row goes off screen. I might only actually cancel the download after a short delay, so it can be resumed without wasting bandwidth if the row comes on-screen quickly again.
I, however, am unsure about a few things:
What is the best way to detect when a row goes off-screen so I can cancel the download?
Is calling notifyDataSetChanged() the best thing to do after the download has completed or is there a better way?
Also any comments on the whole strategy would be appreciated.
I don't think calling notifyDataSetChanged() is really needed... I would do it like that:
store URL as Tag in the view when created/updated
register a listener in downloader thread (async task???) for download keeping reference to the view and the URL
whenever image is downloaded asynchronously, I check TAG in the view and if it matches - i would update the ImageView (important to do it in UI thread, but when using async task, it is given). The image should also be stored on SD card (and every time you request URL you should check if it is not already downloaded).
every time when getView() reuses the view (passed view is not empty) I would check the Tag (old URL), replace it with the new URL and cancel the download of the oldURL.
I think it would be pretty much it (some corner cases might happen)...
I use the getFirstVisible and getLastVisible AdapterView properties to detect the visible rows, and put requests in a fixed size stack.
My project is open source and has a most permissive license, if you want to use it:
https://github.com/tbiehn/Android-Adapter-Image-Loader
-Travis
I found the remote resource managing / fetching in the Foursquared source code to be pretty helpful:
http://code.google.com/p/foursquared/source/browse/main/src/com/joelapenna/foursquared/util/RemoteResourceManager.java
It caches images on disk and handles all 3 of your feature requests. See an adapter for how to use it.
As for canceling a download when a row goes off screen you'll have to handle that yourself

Categories

Resources