I'm trying to an android version of an iOS app I recently launched. I'm struggling with image array. In iOS, add my image files, then call them by creating a var = UIImage(named:"name image file), then I use that.
In Android, I don't know how to access the drawable folder to make an image array that has multiple images, and how to call it in code so it is displayed in the image view on the screen.
I tried but had so many errors. Not many answers on the Net as well, been trying for two days.
For now, I am thinking of a button and an image view on the screen/activity. When I press the button, a random image appears. Could you please tell me how to go about it? (I took a Kotlin course on Treehouse but they only do an array of strings, I used that as a base but just don't know how to work with multiple images at all).
Appreciate your help, thanks.
used below code ..
var cards= arrayOf(R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7);
button.setOnClickListener {
var r = new Random()
var n=r.nextInt(7)
imageview.setImageResource(cards[n]);
}
Related
I'm newbie and I need help I can't resolve problem i don't know what to do. Very confusing it and need help (if you explain about it i'm very very thank you).
I watching Kotlin Project - Kotlin Android Instagram Clone using Firebase - Kotlin Instagram Clone
He using ArthurHub/Android-Image-Cropper (but it is old version right ?), I find some new version is CanHub/Android-Image-Cropper.
CropImage.activity()
.setAspectRatio(1,1)
.start(this#AccountSettingsActivity)
I can't use this code and i don't know how to resolve this problem, be utterly mystified.
Help me please Thank you very much.
Best regards.
(you can blame me, but not harsh i'm Newbie and I don't know)
I need help I can't resolve problem i don't know what to do. Very confusing it and need help
Since you said you're new I'll help you understand what the project page's example usage is showing you. I can only do this in general, I don't have time to go actually creating an example project to test it myself.
If you run into any issues, you'll have to puzzle it out - that's a big part of coding! It's rare that you'll get a nice simple solution that drops neatly into whatever you're doing, and learning how to make it work for you is an important skill to develop.
If you're ever stuck, always look at the project page (in this case the Github page) for a user guide, and they have a sample project in the repository too, so you can see how it works inside a full app. Don't be afraid to look at it, even if you don't understand it all - the more you do this the more you'll understand
Anyway, here are their examples:
Calling crop directly
class MainActivity {
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
if (result.isSuccessful) {
// Use the returned uri.
val uriContent = result.uriContent
val uriFilePath = result.getUriFilePath(context) // optional usage
} else {
// An error occurred.
val exception = result.error
}
}
I'm going to break the code up here - this first bit is showing you're doing this inside an Activity. You're creating a cropImage object, which is an Activity launcher provided by registerForActivityResult - you're going to call launch on that later to start your cropping Activity.
The lambda in the curly braces is the function that's going to run when you get a result back - this is where you actually do something with the cropped image. In this case they're getting URIs to the image - I don't know what you need, but you can google "get bitmap from URI" or whatever if you need something else.
This whole getResultFromActivity thing is part of the AndroidX Activity and Fragment libraries - if you want to do it this way, you'll need those (and it's a good idea to use them on every project anyway, AndroidX stuff gives you backwards compatibility and saves you work).
Once you've set that up, you can call launch on cropImage to open your cropping Activity. It looks like they're giving you three examples, with different options (you could combine the options, but you only call launch once):
private fun startCrop() {
// Start picker to get image for cropping and then use the image in cropping activity.
cropImage.launch(
options {
setGuidelines(Guidelines.ON)
}
)
They're putting these in a startCrop() function you can call from, say, a button click listener. This one's setting some guideline options on the launched Activity - and like the comment says, it opens an image picker so the user can select an image.
// Start picker to get image for cropping from only gallery and then use the image in cropping activity.
cropImage.launch(
options {
setImagePickerContractOptions(
PickImageContractOptions(includeGallery = true, includeCamera = false)
)
}
)
This one (again, like the comment says - this stuff is there to help you) is setting some options on the picker instead, specifically limiting the choice to gallery images.
// Start cropping activity for pre-acquired image saved on the device and customize settings.
cropImage.launch(
options(uri = imageUri) {
setGuidelines(Guidelines.ON)
setOutputCompressFormat(CompressFormat.PNG)
}
)
This one does a few things: passes the URI of an image to crop (instead of opening a picker for the user to choose), adds guidelines, and sets the output format for the resulting cropped image.
Remember, these are all examples of the different ways you might use this. You can combine options, depending on how you want to configure things - you're setting options before you launch the cropping Activity.
Using CropView
This one looks like it uses a custom View you can add to a layout, which lets you crop an image:
// Add CropImageView into your activity
<!-- Image Cropper fill the remaining available height -->
<com.canhub.cropper.CropImageView
android:id="#+id/cropImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
So you add this to your Activity's layout (might work on Fragments too, maybe with some tweaking - I'm not testing it remember!). The sizing isn't important - you can work out how to make a view as big as you want it to be. It's just a normal View in a layout.
//Set image to crop
cropImageView.setImageUriAsync(uri)
// Or prefer using uri for performance and better user experience.
cropImageView.setImageBitmap(bitmap)
In the Activity, with a reference to the CropImageView in your layout, you call this set image function with the URI of an image you want to use. Or, if you have it, you can set a Bitmap directly. Once it's set on the CropImageView, the user can adjust it
// Subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.getCroppedImageAsync()
// Or.
val cropped: Bitmap = cropImageView.getCroppedImage()
Now this one's a bit trickier - to me, it's not completely obvious how you're supposed to use that first one. So let's actually look at the code in CropImageView, where we can see how it works and read the documentation.
Surprise! There is no getCroppedImageAsync() function, just a croppedImageAsync one (without the get) that looks like it does the same thing. That's fine. If you're using an IDE like Android Studio, it'll probably suggest the right function as you type, and you can read the documentation right there:
* Cropped image based on the current crop window to the given uri.
* The result will be invoked to listener set by [setOnCropImageCompleteListener].
Ok, that looks like what the example is hinting at. You call setOnCropImageCompleteListener with a function to do something with the resulting image, like this:
cropImageView.setOnCropImageCompleteListener { view, result ->
// do something with cropped result
}
and then you call croppedImageAsync (with whatever option parameters you want, check the docs) to start the crop-and-store process. When it's done, that callback function you just set will be called with the result.
And then there's the custom Activity example but that's enough for me - if you really want that, if you can follow what we've gone over so far you should be able to work out the rest.
Since you're already making an app following a tutorial, and it's using a different library (with a different way of doing things) you'll have to work out how to fit this library into your own project. Take a step back and look at what you're doing in the tutorial, what you're trying to achieve (like launching a cropping Activity), and work out how you'd achieve the same task in this library.
A lot of software development is about trying to fit different pieces together like this, and it rarely goes smoothly. Not knowing what you're doing is normal! But then you have to sit down and work it out. Hopefully that gives you an idea of an approach you can take to learning - and there is a lot to learn at first!
Hello i am using MIT App Inventor 2 to make an app that runs on an old (spare) android phone to display the folder.jpg of currently playing movie (later on I also want to do that with music).
I can't filter the path to use, for example this is the result of the json:
{"id":"VideoGetItem","jsonrpc":"2.0","result":{"item":{"id":1,"label":"2012","thumbnail":"image://F%3a%5cMovies%5c2012%5cfolder.jpg/","type":"movie"}}}
I want to keep: image://F%3a%5cMovies%5c2012%5cfolder.jpg which is the dvd cover (and maybe the label: 2012 which is the title of the movie).
I tried various options like making a list but then I still get text with lot of params between '()'
Can someone help me with this?
Thanks
Use the JSON decode block together with the lookup in pairs block. As you can see, it helps to use Do it...
I have something that is doing it for me for starters.
I see a flaw but that's in Kodi(.tv) itself (I am not a fan of the library system in kodi, so I put a folder.jpg in every drawer of a movie, but sometimes kodi doesn't display it and presents me with a video preview of the movie, I have to sort that out with kodi developers)
For now I am glad I have it working,
solution picture
Hello Stackoverflow!
I have an Android project I'm working on, and I've found myself stuck on one of the desired features.
The idea with this specific feature is that the user can load/take 2 images, write a description and then save it as a single object. This object will then be stored in a MySQL DB and will also be shown together with all the previously created objects in an expandable list or similare. The objects themselves dont haveto be clickable, just visible, and they don't haveto be editable after their creation.
-My question is this: How can i add multiple images + text, save it as a single object so that it can be viewed by the user from a list?
This might seem like a silly question for most of you, but I am (obviously) a beginner, and after spending days trying to look for a solution without knowing what I'm even looking for I've decided to ask here!
If anyone could point me in the right direction it would be much appreciated!
I hope this helps.
There are two solutions that come to my mind.
1st one - generating new image
If you are giving the user a list of all images to choose from you can easily take all those images he selected (If you are having trouble implement this, please ask) and move on to next activity or fragment where you will give the user option to enter a description.
In that layout you can have one LinearLayout (inside a ScrollView to enable more images than a screen height) and dynamically add ImageViews that will represent those images selected before.
At the bottom you have EditText for description (but that is less important now).
Once user decides to save this selection you can create new Bitmap from the LinearLayout having all images inside it.
Here is a code below
mLinearLayout.setDrawingCacheEnabled(true);
Bitmap mBitmap = Bitmap.createBitmap(mLinearLayout.getDrawingCache());
mLinearLayout.setDrawingCacheEnabled(false);
You can even put that into the bundle for further "transport"
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray(TAG, bytes);
If you have any questions or maybe need some more snippets feel free to ask.
2nd solution - creating a Map or an Array for your final object
You can have something like this
class MyObject{
List<String> selectedImages;
String description;
}
This list can be list of Bitmaps or maybe even list of Strings representing the urls of images user decided to choose. (I would go with urls)
I'm not sure how you present those images to the user so I might not be able to give you precise solution.
The second solution is more flexible and optimized. It gives you possibility to modify (even though you said it doesn't need to, but just in case) and it saves a hell of space on server (due to saving only strings not whole bitmaps that are mostly repeated in some cases).
Again, I hope this helps.
Okay, I've checked, this question never really got a good answer so I'm doing a follow-up and hope someone can help, doing an Imageview is easy (just a drag & drop and then some styling & referencing) but how do we really put a gif in an activity THE EASY WAY. Let's say our gif's name is mygif1 and it's in the resources folder under raw. I'd like it to play in my activity called activity1 in loop while it lasts, Should I create a class especially for that gif or can I make all the references in activity1, what type do I use in Layout to reference it? Can I make it painless or do i have to create 5 classes and 500 lines of code? An example with this data would be much appreciated :)
See this gist gif utils, use it like below:
final ImageView v = ((ImageView)findViewById(R.id.gif_test));
AnimatedGifDrawable drawable = new AnimatedGifDrawable(getResources()
.openRawResource(R.drawable.gif_image), null, 3);
drawable.setOneShot(false);
v.setImageDrawable(drawable);
Hello all I'm new to android programming and I have a problem.
I created a sudoku app with preset puzzles for 3 difficulties using an e - book's directions.
The thing is that I made a puzzle generator for my puzzles to make them infinite.Now here is my problem :
To save your pre-defined puzzle in the book's example you had to use :
getPreferences(MODE_PRIVATE).edit().putString(PREF_PUZZLE,
toPuzzleString(puzzle)).commit();
toPuzzleString obviously converts the puzzle into a string(which was stored before in a 1 dimention array)
In order to load your saved settings to make the Continue option to work you had to use :
puz = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE,easyPuzzle);
BUT this works for a predefined puzzle stored in a private final string called "easypuzzle" at the beggining of the game.class . My puzzle is generated and stored in a 1 dimentional array when user clicks New Game.As a result I have to pass my generated puzzle as a reference(this is what I think) cause i tried to pass it like this :
getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE,toPuzzleString(puzzle));
and when I close the game or just go back and try to Continue my game generates a new puzzle for me (or this is what I think it is) instead of loading the saved one.
What am I doing wrong? Can anyone help me , or tell me how to pass my puzzle as a reference like in c++?
Thank you all for your time any help would be appreciated....
As mentioned above, your puzzle may not be saved in preferences correctly. In order to check, change the getString(...) method to something like this:
String puzzleFromPrefs = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE, null);
if( null == puzzleFromPrefs ) {
// Then you know that it wasn't saved correctly...
}
This will cause null to be returned if the puzzle hasn't already been saved in the preferences. This will help narrow down where the issue may be.