How do we know if an ImageView is updated in Android? - android

I have a form, where I have a imageview and other two text fields. This imageview will be updated with the pic a user chooses(either from gallery or by taking a pic). I will be saving this image in internal storage and other two text fields and saved image path in SQLite DB.
User has option of not choosing any image. In that case I should not save any image, coz I simply be wasting storage. For that how do I distinguish when a user chooses or not to upload any image?

int NEW_PIC=1;
int NO_CHANGE=0;
initialise
img_camera.setTag(AppConfig.NO_CHANGE);
when Changed set
img_camera.setTag(AppConfig.NEW_PIC_ADDED);
if ((int) iv_userImage.getTag() == NEW_PIC_ADDED) {
//New Image Added
}
else{
//Not New
}

Related

how to save imageview to sharedpreference using kotlin?

so I'm trying to save an Imageview to sharedpreference so when the user chose an image from the app it self, the Image will be saved and set to the imageview id if that makes sense! and when the user exit my app and reopen it the image will be the same one he saved?
I tried this code but it's giving me errors such as for input string " "
My SharedPreference
CompassBtn.setOnClickListener{
val prefCompass = getSharedPreferences("Drawable", Context.MODE_PRIVATE)
val editor = prefCompass.edit()
editor.putInt("ic_compass", ic_compass.setImageResource(R.drawable.ic_compass1).toString().toInt())
editor.commit()
}
**this is how am trying to retrieve it **
val prfCompass = getSharedPreferences("Drawable", Context.MODE_PRIVATE)
prfCompass.getInt("ic_compass", 0)
please help and thanks in advance
First of all, if you are new to Android Development, please check Picasso for uploading images.
In short, it makes it easier/faster to upload images using resource id/url.
Your question really depends on the type of images you want user to select in the future.
1) If all of the images that can be selected are already in the application you can just save the resource id of image in SharedPreferences as an int
val sharedPref: SharedPreferences = context.getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
// Resource id is the int under drawables folder ->R.drawable.myImage
fun save(KEY_NAME: String, value: Int) {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putInt(KEY_NAME, value)
editor.apply()
}
fun getInt(KEY_NAME: String): Int {
return sharedPref.getInt(KEY_NAME, 0)
}
2) If you are letting user to select from gallery (this is the tricky part) inside onActivityResult(which gets called after user selects an image with the argument, Intent data, that includes the information on image). Accessing the data of intent (data.getData()) will give you the URI. Then you need to find the path of the image (path where the image is stored in user's phone) and save it to SharedPreferences. I will leave getting the path of image as a challenge to you. And when you want to upload the image you can just;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
myLayoutItem.setBackground(drawable);
3) If you have a server you can just upload your images there and store the url as a string in SharedPreferences/associate url with an image attribute of user. And use Picasso to display the image.
As you have mentioned in the comment that you have the images in the res/drawable folder, and you want to save the selected image by the user and load the same when the user reopens the app.
So, the thing you have to do is save the resourceId in the preference and use that value.
It will be something this.
//Suppose this is your selected drawable, you need to save its resourceId to shared preferences which is INT value
val resourceId: Int = R.drawable.your_drawable_image
//save resouceId to sharedPreference
//You can do this to set the Image in the ImageView
your_imageview.setImageDrawable(getDrawable(resourceId))
I hope this will be of any help to you.

how to select each image uniquely in android using appium

Hi i am new on automating mobile apps, some help would be appreciated.
i want to select some images from gallery through app, however all the images are having same class name and i can select only one image, while trying to select other image it just deselects first image and select that image again.
driver.findElement(By.xpath("//android.widget.TextView[#text='pictures']")).click(); //image selection // select folder
if(Product.equalsIgnoreCase("Photobooks"))
{
// number of images to be selected is equal to
for(int i=0;i<40;i++){
Thread.sleep(5000);
driver.findElement(By.className("android.widget.ImageView")).click();
}
This code goes clicks on one folder containing images and tries to selelct 40 images from the gallery to design book.
The problem is that you keep finding the same element.
What you need to do is to get the list of ALL the images, and then click on every image from that list:
List<WebElement> imageList = driver.findElements(By.className("android.widget.ImageView")).click();
if(Product.equalsIgnoreCase("Photobooks"))
{
// number of images to be selected is equal to
for(int i=0;i<40;i++){
imageList.get(i).click();
Thread.sleep(5000);
}
}

Multiple Image Crop Android

I am making an application that allows the user to upload multiple images from the gallery. The user selects the photos and they are displayed in a list view and the user clicks an upload button. It was working fine until i discovered some images were changing orientation on the list view and on upload.
I asked around about the issue and someone told me that i had to allow for cropping before upload. Should i make the user crop every selected image from gallery or how should i go about it?
if (requestCode == SELECT_MULTIPLE_PHOTOS) {
String imagesArray = data.getStringExtra(EXTRA_SELECTED_PHOTOS);//get Intent data
//Convert string array into List by splitting by ',' and substring after '[' and before ']'
List<String> selectedImages =
Arrays.asList(imagesArray.substring(1, imagesArray.length() - 1)
.split(", "));
Log.d(TAG, "Selected images is " + selectedImages.size());
mPhotosPathList = new ArrayList<String>(selectedImages);
Log.d(TAG, "Selected images path is " + mPhotosPathList.size());
}
Get the image orientation. If it is not in the correct orientation, the get the correct orientation, Try this link

Unwanted image being saved in gallery

I have used an answer from here
How should I give images rounded corners in Android?
However when created the picture with rounded edges it is being saved in my camera gallery as well as in a custom location that I have specified, any reason why this would happen or anyway to get rid of it automatically?
I don't want a picture being saved here.
Images are not saved in your device gallery - the gallery just displays all the images that are on your phone, UNLESS they are in storage associated with particular application. Use the following method do determine where you should save the images.
private File getAbosoluteFile(String ralativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), ralativePath);
} else {
return new File(context.getFilesDir(), ralativePath);
}
}
It will store them in external storage associated with your application and they will seize being displayed in the device's gallery. Try this out with new image, because images are cached for a long time in the gallery application.

Android: How to manage images (existing vs non existing) in the project

The title seems ambigious but my goal is simple to understand.
I have a certain number of images in my project under res/drawable-hdpi.
In my app, I am geting a list of names from the server and I am saving them in the local databse.
Each name corresponds to the name of the image with ".png" at the end.
I am using the Galery and the user could choose one of the names from the database by clicking on his corresponding image.
Everything is fine until here.
But now, lets say that a new name will be added on the server side, so the image will not be in the application. In this case I have to update my app and put the right image inside.
In order to avoid that the user sees a "black -image" in the galery (because the image does not exists), I would like to create this image with android.
I am actually able to catch if the image does not exists in the project and to create a new one (white background with the name in the middle).
Now, the problem is how and where to store this new image. Apparently, it is not possible to store this in res/drawable folder. So, where and how to store?
Here is the part of the code where I am creating the new image:
if (imageId == 0)
{
Bitmap journal_template = BitmapFactory.decodeResource(context.getResources(), R.drawable.journals_template).copy(Bitmap.Config.ARGB_8888, true);
Canvas myCanvas = new Canvas(journal_template);
Paint myPaint = new Paint();
myPaint.setColor(Color.BLACK);
myPaint.setTextSize(25);
String journal_name = publicJournalsNameSystem.get(i).toLowerCase();
Paint textPaint = new Paint();
textPaint.setARGB(200, 254, 0, 0);
textPaint.setTextAlign(Align.CENTER);
int xPos = (myCanvas.getWidth() / 2);
int yPos = (int) ((myCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
myCanvas.drawText("Your text", xPos, yPos, myPaint);
try {
FileOutputStream out = new FileOutputStream("/journals_"+journal_name+".png");
journal_template.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
In order to use images from a web server, you need to first download the image on to the device then store it.
Here is a good example of how to download the image.
The developer website has methods on how to store data to the device. You will either want to store it internally or (more preferably) externally to the SD card. It would be recommended that you first check if the SD card is mounted on the device and usable. Store the image internally if the SD isn't available.
You must then keep track of the image's URI after downloading. Temporary images can simply be tracked with the app then deleted upon onDestroy(). Permanent image URIs should be stored via either SharedPreferences, SQLite database, or ContentProvider. A ContentProvider is preferred as it adds a layer of abstraction for how you want to store the image. It's usually backed by an SQLite database anyway, but other apps don't need to know that. It also allows other applications to easily access the image if you want (say, the Gallery for example). You can prevent access if you choose. SharedPreferences is easier to implement if you only have a few images. It's least recommended though.

Categories

Resources