Ok, I am able to load a specific image into ImageView from my SD card...no problem. But I need to take a step further and add a button load the next image (move/copy/delete previous image). I can add the button, etc but how do I retreive the 'next' image without knowing filenames? Here is the code so far. I would have liked to use Gallery but it has been deprecated and I can't seem to make anything else work. Thank You
package com.demo.ShowSDImages;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class AndroidBitmap extends Activity
{private final String imageInSD = "/sdcard/er.PNG";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);
}
}
i think you can give a try to this sample here..Check this link
here i think the below code can help you in getting the images .
int imageCount = sdcardPath.listFiles().length;
for (int count = 0; count < imageCount - 1; count++) {
Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[count]
.getAbsolutePath());
In the sample instead of using the thread or timer use the the same logic that is using of the showNext() in the button click event
Give a try. Happy Coding
Related
I am really new to coding and would like to ask a question. I grabbed some code from mkyongs website and would like to know something. The code has a button that changes an image to a different image. I would like to have 2 buttons that changes the image. First button changes the image to a different image, second button changes that image again. I would like to know if I create a new activity and use all the same code or if I attach the onClickListener again on the same activity. Thanks :)
Here is the code for the button and image change (Activity)
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity2 extends Activity {
Button button;
ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.background3);
}
});
}
}
It seems you are trying to Swipe images, the below example is from this site
They created 2 methos (next and previous) on the same activity, that will the images
public void next(View view){
Toast.makeText(getApplicationContext(), "Next Image",
Toast.LENGTH_LONG).show();
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
imageSwitcher.setImageResource(R.drawable.ic_launcher);
}
public void previous(View view){
Toast.makeText(getApplicationContext(), "previous Image",
Toast.LENGTH_LONG).show();
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(out);
imageSwitcher.setOutAnimation(in);
imageSwitcher.setImageResource(R.drawable.ic_launcher);
}
However it seems your new to android Development so I recomend to read the basics and try them first , there are hundreds of tutorials on the internet just google. You can start from android site
I have html content and setting this into (android) textbox as follows
textbox.setText(Html.fromHtml(myhtml));
HTML content contains some img tags as well, I do not want display images, currently it displays empty square boxes where ever there is a img tag. How do I hide those square boxes?
You could do a regex replace <img.+?> on htmlString.
textView.setText(Html.fromHtml(htmlString.replaceAll("<img.+?>", "")));
Untested
Update:
since images can look like:
<img ...></img>
and
<img... />
This solution will match both cases:
String htmlBody = htmlString.replaceAll("<img.+/(img)*>", "");
textView.setText(Html.fromHtml(htmlBody));
This works
Create Html.ImageGetter to return empty image as follows
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
public class EmptyImageGetter implements Html.ImageGetter {
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
#Override
public Drawable getDrawable(String source) {
return TRANSPARENT_DRAWABLE;
}
}
and then create a static instance
private static final EmptyImageGetter EMPTY_IMAGE_GETTER = new EmptyImageGetter();
set this into text view
textView.setText(Html.fromHtml(myhtml, EMPTY_IMAGE_GETTER, null));
i recently started programming in Android and i came across a little problem.
What i'm trying to do is:
I have NewsActivity and a NewsRows.class (in the same package). So the news activites just creates a new NewsRows object and tells it to fill the TableLayout with new rows.
It works fine as long as i try to add an image from a resource... The app just keeps crashing.
The debugger tells me it can't find the resource but i can't find out why!
My code is here:
News Acitivty
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.os.Bundle;
public class News extends Activity {
NewsRows rowClass = new NewsRows();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
NewsRows.createNewsEntries(this);
}
}
NewsRows.class
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class NewsRows {
static TextView title;
static TableRow tRow;
static TableLayout tLayout;
public NewsRows() {
}
public static void createNewsEntries(Activity contextActivity) {
ImageView image = new ImageView(contextActivity);
image.setBackgroundColor(R.drawable.myimage);
tLayout = (TableLayout) contextActivity.findViewById(R.id.NewsTable);
for(int a = 0; a < 100; a++) {
tRow = new TableRow(contextActivity);
title = new TextView(contextActivity);
//tRow.addView(image);
title.setText("This is a test.");
tRow.addView(title);
tLayout.addView(tRow);
}
}
}
EDIT:
The line
image.setBackgroundColor(R.drawable.myimage);<br />
Is actually supposed to be:
image.setImageResource(R.drawable.myimage);
You're trying to set an image into a background color:
Change this:
image.setBackgroundColor(R.drawable.myimage);
to this:
image.setBackgroundResource(R.drawable.myimage);
Fun fact:
TableLayout tLayout = (TableLayout) findViewById(R.id.NewsTable);
TableRow tRow = new TableRow(this);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.myimage);
tRow.addView(image);
tLayout.addView(tRow);
If i put this code right in the News.Activity it works...
You can't set a drawable for as a background color of your ImageView, that's why you have the resource not found exception on your logcat!!
image.setBackgroundColor(R.drawable.myimage);
change it to this:
image.setBackgroundResource(R.drawable.myimage);
Also try to change this
public static void createNewsEntries(Activity contextActivity)
with
public static void createNewsEntries(Context contextActivity)
I tried all the answers that i found in the Android section with out any success before i post this question...
for some reason the image quality in the device is poor and in Eclipse and in the virtual device is very good
look on the screenshot example:
example http://img714.imageshack.us/img714/1358/84044294.jpg
what should i do?
i try to make the image with 72dpi and 300 dpi, the PNG/JPG resolution is 1280x800
but nothing works...
Please help!!
This is my LiveNewsActivity.java maybe i'm doing something wrong?
package com.prog.livenews;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class LiveNewsActivity extends Activity {
/** Called when the activity is first created. */
//############################
//######### VARS #############
Button login;
Button register;
//############################
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap gradient = BitmapFactory.decodeResource(getResources(), R.drawable.bg, options);
findViewById(R.id.frameLayout1).setBackgroundDrawable(new BitmapDrawable(gradient));
login = (Button) findViewById(R.id.main_login_button);
register = (Button) findViewById(R.id.main_register_button);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.prog.livenews.LOGIN"));
}
});
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.prog.livenews.REGISTER"));
}
});
}
}
try it:
put it before setContentView(layoutId) on your first activity
getWindow().setFormat(PixelFormat.RGBA_8888);
and after call setContentView(layoutId) in your first activity you put the bellow code:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
options.inScaled = false;
options.inDither = false;
options.inPurgeable = true;
Bitmap preparedBitmap = BitmapFactory.decodeResource(yourAppName.getSharedApplication().getResources(),
R.drawable.bg, options);
Drawable background = new BitmapDrawable(preparedBitmap);
((LinearLayout) findViewById(R.id.yourLayoutId))
.setBackgroundDrawable(background);
and finnaly you put your xml images in res/drawable "if you have xml images" folder and the png, jpeg and others normals images you put in res/drawable-hdpi folder.
Hope this helps.
I tried all the answers that i found in the Android section with out any success
It'd be helpful if you stated which approaches you've tried too, but Marc's comment is correct. You need to make sure your image and window are both higher bit depth (probably ARGB_8888). See this article for more info and you can possibly enable dithering. Putting this in onCreate should take care of a lot:
getWindow().setFormat(PixelFormat.RGBA_8888);
O.K guys, I found a solution...
As i understand... when saving the gradient image in Photoshop, Photoshop will try to optimize the image size by removing the Alpha channel.
So you need to delete one pixel from the top (left/right) corner or the buttom (right/left) corner
and then Photoshop will "see" that the image have an Alpha channel and it will work on the Device...
be aware... some devices are not able to show gradient color perfect..
Hope that was helpful! Thanks guys for your help!
Try using the High Quality Image. Can you confirm whether background is also containing the Globe Image and text on it? Is background is having gradient color or not ?
I have a image button and i want that image button when pressed it changes the text in a textbox and it changes a image to a different image how do i do this?
You need an OnClickListener: http://developer.android.com/reference/android/view/View.OnClickListener.html
When clicked your text can be changed with (something like) text.setText("new text");
I'll find a link in a minute which will help more.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class TestesetetActivity extends Activity {
/** Called when the activity is first created. */
TextView textview = null;
ImageButton buttonResume = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonResume = (ImageButton) findViewById(R.id.imageButton1);
textview = (TextView) findViewById(R.id.textView1);
buttonResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textview.setText("test");
buttonResume.setImageResource(R.drawable.push_pin);
}
});
}
}
In the xml file, you can give the image an onClick attribute, which calls a method in the java class, which calls the xml resource, and passes it the id of the ImageView.
In that java method, you can use
((ImageView) view).setImageResource(int id)
or
setImageDrawable(Drawable d)
to change the image.
Likewise, you can identify the TextView you wish to change using, for example,
TextView tv = (TextView) findViewById( id )
with id being the id of the TextView you wish to find.
You can then use
tv.setText(String s)
to set the text in this view.
In your OnClickListener, you could use
button.setBackgroundResource(YOUR_BUTTON_ID);
or
button.setImageResource(YOUR_BUTTON_ID);