I am writing an app that allows user to either take a picture with the camera
or choose an image in the gallery. I have a Fragment whose the layout displays
two boutons, depending on the button pressed I use intent either to start the
camera or open the Gallery. It works fine.
My purpose now is to do that with MVP. As I see things
my fragment is the View
the place from where images come from is the Model so in this case the models
would be getting images from the gallery or with the camera
a Presenter asks models to give him a picture and forwarding the image
to the fragment
The problem is model objects are Pojo classes and to retrieve an image from the
Camera or Gallery the class needs to implement startActivityForResult to retrieve
the photo taken/choosed.
How can I get image from camera/gallery with a class that is not a Fragment or
an Activity ?
How can I move code for camera/gallery in POJO class ?
Is it overall a good idea ? I did not find any MVP examples where retrieving
images was done out of a Fragment or an Activity ?
Note: Maybe is such an architecture possible with RxAndroid but I wish I could do that first without third party librairies.
Thanks for all your suggestion
How can I get image from camera/gallery with a class that is not a Fragment or an Activity ?
You have to do all this stuff from your presenter, not from your POJO class. Here you want to access an android resource, so pass the call back to the 'view-layer' from 'presenter-layer' and get response from system. Once the response is received in the onActivityResult method of Fragment/Activity, pass it into the presenter. cheers :)
Related
I need to open camera and take picture then get the result as file path and save it somewhere .In activity it is simple but in RecyclerView its a little bit hard to do I guess
its a nested RecyclerView by the way ,may not be that important but may be helpfull
Does any body know how to do this ?
Best way to do this is to use call back to parent activity and do all open camera related task there
i have a main activity and second activity. on click of image button in main activity, it directs to second activity and shows a 6 avatar image buttons. i should select one of them and display back onto main activity. Can someone help me do this by implementing parcelable interface to pass data between activities?
Thanks in Advance.
If you're passing a model back to your main activity you can convert your model automatically to implement the Parcelable interface via this tool.
If you're trying to pass back a large object in your intent you might get TransactionTooLargeException thrown. It really just depends on the data you're passing.
I'm using android studio to make an instagram app, which for the most part works fine.
I can get my most recent posts into a listview, but I want to be able to click on a particular post and bring up a detail view of just that post.
I've tried doing it by passing the id through an onclick into a DetailView activity using Intent, but this just gives me a blank page. I also tried using a fragment but I couldn't get that to work either. The adapter uses ArrayList and I was trying to pass that using parcelable and then serializable but neither of those worked.
Honestly, this is my first time using android studio or even making an app for android so I'm at my wits end here.
link to GitHub for code: https://github.com/mckarlsson/Clone/tree/DetailView-Latest
This may not be the best way but here is how I usually do this:
for displaying images without blocking the UI thread I use a image loader library. For instance Android Volley Image Loader. The advantage of this, for instance is that if it loads something it keeps it in cache and it's retrieved from cache when later needed
Since the image loaded is most likely cached based on its url, I pass the image's url as a Bundle parameter to a DialogFragment, when tapping on list item, and then in the fragment, I simply use the ImageLoader library to load the image into an ImageView.
This has some advantages, for instance, next time you start your app, the cached images will load almost instant, same for tapping on them.
In my apps, i need to pass image of image view from one activity to another. I know there are different kinds of way to pass image from activity to activity. But i want to know the best approach to do that. Previously i tried to pass image by getting the Bitmap of imageview from first activity then put in as putextra to the intent then extract the bitmap by getPercible to second activity. This was working good in Lollipop but getting error in Nougat. Now, i am trying to pass the bitmap as byteArray, but this is more unreliable than previous one. I don't know how to overcome this situation. Please help me in this context.
Where do you get your image from?
I would advise you to store it locally and only pass the path to it to the other activity. In this activity just retrieve the Bitmap again from your local storage.
I am developing an Android application and can't quite figure out the best way of implementing a 2-level image cache that can be shared among multiple activities w/in a single application.
Example:
Application has 3 activities (A, B, and C) and for the sake of argument lets say that A calls B and B calls C and C calls A. Each activity displays an image downloaded from the web and I'm using asynctask to download and display images w/in each activity - easy enough. Now I'd like to add an image cache to avoid multiple downloads of the same image.
Right now each activity starts a new instance of a simple asynctask that downloads the image and updates the view appropriately. Obviously its easy enough to update the basic asynctask to check the image cache before proceeding to download and to update the cache once the download is complete but I'm stuck on how/where to create and initialize the cache. Any thoughts would be appreciated.
You can add this to your app's manifest:
application android:name
="MyApplication" (...)
You can then create a class that has the name "MyApplication". You can then use that class across your activities. Check, before making the async call, if you already have a proper image to use. If you have, you use the one "cached", if not, you can get a new one. You can try something like this (in this case to get some random strings):
ArrayList myStrings =
((MyApplication)
this.getApplication()).getRandomStrings();
Hope this helped you. :)
Edit: Don't forget to the create your "MyApplication" like this:
public class MyApplication extends
Application