I have 3 images, named img1.png, img2.png, img3.png .
Is it possible to load them once at a time clicking on the same button, getting the number of their name?
first button click -> read image names -> img1 is first
second button click -> read image names -> img2 is second
third button click -> read image names -> img3 is third
EDIT:
I explain better, I'm just trying to figure out what's the best approach:
the example above would allow me to just create a method and avoid creating an array of images.
Second approach is to create an array.
Third approach, create a counter and load an image based on the counter value.
Other approaches?
You can use a variable say
int nowDisplayNumber=0;
String imageName="img"+ ++nowDisplayNumber+".png";//or other approach for construct the image id, or image name
.....//display your image use imageName here.
Please notice that if you do not want the image displayed from the beginning again every time you restart your application, you may need to store the latest image number into a file and read it when application begins
int[] imagename = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
In onClick()
switch(view.getId) {
case R.id.button1 :
button1.setBackgroundResource(imagename[0]);
break;
case R.id.button2 :
button2.setBackgroundResource(imagename[1]);
break;
case R.id.button3 :
button3.setBackgroundResource(imagename[2]);
break;
}
Related
Problem:
I wanted to create a directory such that when I click on a button for one time the directory must be created with the name "Node_1". when I click on the same button once again the directory must be created with the name "Node_2".
Please do not bother about the button. I need the code where the count must be incremented as stated above.
The below sample code will create a directory with the name "Node_1" When I click once.
But when I click button Once again, it will create a directory with the same name "Node_1" again. But I wanted to create with the name "Node_2".
String src = "C:\\Users\\DELL\\Documents\\NetBeansProjects\\src\\Nodes\\Node_1\\n1_sample.txt";
Hope, this will help:
int clickCounter = 0;
void onClick() {
++clickCounter;
.
.
String src = "...\\Node_" + clickCounter + "\\...txt"
}
Note: Learn to ask question in a better way. Share what you want to achieve, what you've tried already, and what problem you are facing, and also keep it simple.
I am fairly new to Java, and so some of the classes like Color Model and JAI are not familiar to me. I have a tiff image I am reading into a program in Java. This is my read-in code:
BufferedImage img = null;
String input[] = {"LE70160412002112EDC00_sr_band5", "LE70160412002144EDC00_sr_band5"};
String filetype = "tif";
File file = new File(input[0] + "output.csv");
int numFiles = 0;
while (numFiles < 2){
if (filetype == "tif"){
FileSeekableStream stream = new FileSeekableStream(new File(input[numFiles] + ".tif"));
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ParameterBlock params = new ParameterBlock();
params.add(stream);
RenderedOp image1 = JAI.create("tiff", params);
img = image1.getAsBufferedImage();
}
}
To be clear, other things are done further down in the while loop that I excluded such that the files are not overwriting each other. The problem I am having is not being able read in the file and get further into the loop. I had a tiff file that only had black and white pixels (0 or 255 red value for all pixels), and the code ran correctly because the file supplied the Color Model. The new tiff file I am trying to use is a greyscale picture (0 to 255 red value for all pixels), and every time I run the code it gives me the following error message:
Exception in thread "main" java.lang.IllegalArgumentException: No ColorModel is supplied and the image ColorModel is null.
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2500)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2546)
at Soda.DoStuff.doStuff(DoStuff.java:60)
at Soda.Driver.main(Driver.java:6)
My first instinct given the error message is to create a new Color Model. There may also be a better way to use JAI to import the tiff file such that it supplies the Color Model for the greyscale image. My end goal is to get the red value for each pixel in the image, so I do not want the data coming in to be altered from it's original form.
Any help I can get would be much appreciated. I am open to any suggestions.
EDIT:
Someone commented to try and use the getDefaultColorModel class inside the PlanarImage library, so I changed the bottom line of the code to this:
cm = PlanarImage.getDefaultColorModel(0, 1); //
img = image1.getAsBufferedImage(null, cm);
This also did not completely work but provided a different error message:
Exception in thread "main" java.lang.IllegalArgumentException: SampleModel and ColorModel parameters must be non-null.
at com.sun.media.jai.util.JDKWorkarounds.areCompatibleDataModels(JDKWorkarounds.java:363)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2505)
at Soda.DoStuff.doStuff(DoStuff.java:64)
at Soda.Driver.main(Driver.java:6)
I have extensively read through https://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PlanarImage.html to learn about the PlanarImage class, but I still cannot figure out how to properly format a ColorModel. (0,1) creates a color model with 8 pixel bits and 1 component. I also tried with (1,1) which creates a color model with 16 pixel bits and 1 component. Both provided the same error message above.
EDIT2: Unfortunately, I cannot link the image itself. However, I can tell you how I got the image from USGS. Forewarning, getting this image requires you to make a free account, and then afterwards you have to 'order' the picture from USGS, which is simply they need to process the request and give a download link for a zip file. It WILL take some time before you can actually access the picture. I also suggest making the account first because it will not let you start the image checkout process until you have an account made.
Using this link, http://earthexplorer.usgs.gov/, you pick a point on the map, then set the date range so that it ends on 12/31/2002 (the start date does not matter). The go to the Data set tab where under the "Landsat Archive" bullet, you will hit the checkbox for "Landsat Surface Reflectance - L7 ETM+". Hit OK on the dialogue, then hit "Results" at the bottom of the screen.
Once you have signed into your account and done this search, you should see many images on the left side of your screen with similar names to the filenames in my code above. You want to hit the shopping cart next to one of the images (You only need one, my whole project required 2, but for the purposes of reading in the file, that's not necessary). The shopping cart should turn green. Then in the top right corner there is a link to an Item Basket. You hit "Proceed to Checkout" and "Submit Order" on successive screens, and then you wait for an email from USGS.
Finally unzip the file, and you should have about 10 images. As you can see in the code, I am using the image with the name "sr_band5", but I believe any of the bands are greyscale which I cannot read in. Hope this can help.
I am doing a reverse engineering of an android application.I need to change the Images used in application. After searching in application i came to know that images are set using the method setImageResource().
My problem is that in the code of an app setImageResource() method have been passed an integer value which is not an id of any resource in the application .. Here is a sample code:
{
private ImageView h;
case 0:
this.h.setImageResource(17301543);
break;
case 1:
this.h.setImageResource(17301533);
break;
case 2:
this.h.setImageResource(17301568);
}
I have searched these integer values in R.java file of an app but no id is matching with these integers.According to my understanding any id of android resource is of 10 digits decimal number represented in R.java of in public.xml in hex form.
I am not able to understand what are these values are .. lake "17301543" .
SO can anyone please tell me what these integer values could be or does they represent something else.
Thank u in advance . . . .
Put these three images in your res/drawable folder with name image1, image2 & image3 and use this instead
{
private ImageView h;
case 0:
this.h.setImageResource(R.drawable.image1);
break;
case 1:
this.h.setImageResource(R.drawable.image2);
break;
case 2:
this.h.setImageResource(R.drawable.image3);
}
You never use integer values here. You use the ids, which are autogenerated from your resource files (and a few built into android). For example, if you have a file myImage.jpg in your drawables folder, you'd use R.id.myImage
You have to provide resource id of your drawable R.id.imageId.
Also you can pass 0 to setImageResource, this will clear the image view.
For example, if you have a switch case where you want to set image in 2-3 cases and in rest you want to clear the imageview.
case 0:
this.h.setImageResource(R.drawable.image1);
break;
case 1:
this.h.setImageResource(R.drawable.image2);
break;
default:
this.h.setImageResource(0); //Passing 0 to setImageResource will clear the image view.
I've been trying to get an icon of some APK file, but with no success.
The thing is, I have the APK file path and using that, I want to get its icon (as a Drawable).
Thanx upfront.
Have a look at PackageItemInfo. With that, you can read out the icon for any app installed on the device!
Or even faster: Use
Context.getPackageManager()
to receive a reference on a PackageManager. On that, you can call:
getActivityIcon(Intent i)
The only thing you have still to do is packing the package path in the intent.
Expanding on Matthias' answer, since my original assumed ic_launcher.png:
int icon = 0;
// if you don't know the icon's name:
icon = (new PackageItemInfo()).icon;
// if you do know the icon's name:
icon = R.drawable.ic_launcher; // or whatever your resource id is
Drawable d = getResources().getDrawable( icon );
My issue is this (Don't let the paragraph intimidate you, my question is quite simple I believe) :
In the MainActivity(A), I have a listView. When I click on an item in the listView, I go to a second activity(B). In activity B, I have a button that allows the user to take a picture. Now I save this picture under a filename. Since I want each listItem's picture to be unique, I make the filename of the picture that item's position. For example, if I click item#1 in the list, the corresponding picture's name is "1". This all works very well except when I delete an item. Then, when I add another item at the same position, my code loads the existing picture for that item. e.g.- If I have a picture called '2' saved for an item2, it's fine. when I delete this entry and add another item, which is the new item2, my code checks, "does the picture name '2' exist?". If yes, then set this entry's picture to that. Obviously this is a different entry and I don't want this to happen. Possibly, I could delete the picture along with the item, but I have no clue how to do this.
I hope I explained my logic well. The only reason I am using the position is because that is the only unique thing about the item that I can find. By the way(not sure if this is relevant), I am passing the item position as an extra in the intent between Activity A and Activity B.
How can I solve this issue or use a workaround? Thanks!
EDIT 1:
Facepalm. By looking at comments, I realize that deleting it(as I stated earlier)^^, is the way to do this. Could someone tell me how to do this then?
This the code I am using to save the Image:
private void setImage() {
if (loadPicture(getIntent().getStringExtra("position"), bitmap) != null) {
// Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(loadPicture(
getIntent().getStringExtra("position"), bitmap));
}
}
private void savePicture(String filename, Bitmap b, Context ctx) {
try {
// ObjectOutputStream bos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
// bos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true)
// Toast.makeText(this, "returned true",
// Toast.LENGTH_LONG).show();
// bos.flush();
// bos.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Cheers.
The answer is in the question : delete the file when you delete the item. File.delete() is the method you're probably searching for. You'll also have to rename the other files if the item is not the last one in the list, though.
Another, easier way would be to use a simple counter to name your files, and associate each item in the list with the file name with the help of a Map.
I don't want to step on JB Nizet's answer but I will give another suggestion. Using SimpleCursorAdapter and a SqliteDatabase you could avoid having to manage any kind of numeric key. Just hold the path to the file in the database along with any other related information for the user, setup your ViewBinder and you're off to the races. A binder is a very flexible way to render data and even conditionally render View objects.
The way I solved this issue is by incrementing a counter every time I added the item, then saving this counter in the internal memory. This way, it was not based upon the position and would not get messed up when I deleted an item. I hope this continues to work as I make modifications to my application. Thanks to all for help.