I want to load images from external storage to Gallery in android. I have the file path. I want to display all the pics of Mix folder in gallery.
File file = new File(Environment.getExternalStorageDirectory().getPath() +"/Gallery/Mix/");
I am able to load the images in gallery when I put the images in drawable folder but where and what changes should I make So I could display the images from Mix Folder.
PS. I know Gallery is deprecated but still I need to use it.
MainActivity.java
public class MainActivity extends Activity {
Gallery gallery;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery)findViewById(R.id.gallery1);
imageView = (ImageView)findViewById(R.id.imageView1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT).show();
imageView.setImageResource(ImageAdapter.ThumbsIds[position]);
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return ThumbsIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if(convertView==null){
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(600,550));
imageView.setPadding(4, 4, 4, 4);
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.ppic1,
R.drawable.ppic2,
R.drawable.ppic3
};
}
Related
I've created a Fragment that should display some emojis in my GridView, when I try to put the URLs statically outside the public void call method the images get displayed successfully however when I put the code inside this method, the images don't display, I'm doing this for testing as I'm going to make it dynamic, but this is just for testing.
So, appreciate your assistance to advise why the images are not appearing inside this method specifically.
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment {
ArrayList<EmojiModel> emojieModels = new ArrayList<>();;
ArrayList<String> imageId = new ArrayList<>();
View grid;
GridView gridView;
ImageAdapter2 imageAdapter2;
public BottomSheetFragment() {
// Empty Constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.gifts_layout_2, container,
false);
gridView = (GridView)rootView.findViewById(R.id.grid_view2);
getEmojies();
imageAdapter2 = new ImageAdapter2(getActivity().getApplicationContext(),
imageId);
gridView.setAdapter(imageAdapter2);
return rootView;
}
public void getEmojies(){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
Communicator.getInstance().on("subscribe start", new
Emitter.Listener() {
#Override
public void call(Object... args) {
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
}
});
}
});
}
public class ImageAdapter2 extends BaseAdapter {
private Context context;
private ArrayList<String> emojieImages = new ArrayList<>();
// Constructor
public ImageAdapter2(Context context, ArrayList<String> emojieImages) {
this.context = context;
this.emojieImages = emojieImages;
}
#Override
public int getCount() {
return emojieImages.size();
}
#Override
public Object getItem(int position) {
return emojieImages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView imageView;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smiles_items_layout, null);
holder.imageView = (ImageView)
grid.findViewById(R.id.smile_image_view);
Picasso.with(getApplicationContext())
.load(emojieImages.get(position))
.fit()
.centerInside()
.into(holder.imageView);
notifyDataSetChanged();
return grid;
}
}
}
That's because your method runOnUIThread is asynchronous.
To make it work you should set the adapter after the images are loaded:
#Override
public void call(Object... args) {
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageAdapter2 = new ImageAdapter2(getActivity(), imageId);
gridView.setAdapter(imageAdapter2);
}
Also note that you don't need to call runOnUIThread inside a Fragment, it is already running on that thread.
Maybe your image access problems
https://static.pexels.com/photos/247932/pexels-photo-%20247932.jpeg
Can not access
I am working on a setup screen that allows the user to change the color theme of the app. I want to use a GridView of colored square images for the user to choose from. Right now I have a toast message that pops with the position in the color image array when clicked. The following opens my theme activity from a setup menu:
themeButton = (ImageButton) findViewById(R.id.imBtnAccentColor);
themeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SetupContactActivity.this, ThemeActivity.class);
startActivity(intent);
}
});
Here's an image of the GridView that the user will choose a theme color from.
gridViewTiles
Theme Activity:
public class ThemeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(ThemeActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is an ImageAdapter class that helps build the image view based on the number of images in my array of images:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// If it's not recycled, initialize some attributes.
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(320, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// References to the color images
private Integer[] mThumbIds = {
R.drawable.orange,
R.drawable.yellow,
R.drawable.yellow_orange,
R.drawable.brown,
R.drawable.medium_grey,
R.drawable.dark_grey,
R.drawable.pink,
R.drawable.light_blue,
R.drawable.medium_green,
R.drawable.purple,
R.drawable.blue,
R.drawable.brownish_green,
R.drawable.dark_purple,
R.drawable.dark_blue,
R.drawable.green
};
}
I follow some tutorial "How to use GridView in android" and try to make Gallery....
but why?
I already did same with tutorial did....
this file GridActivity.java :
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
and this for file ImageAdapter.java :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId (int position) {
return 0;
}
public View getView(int position, View converView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.image1
};
}
there's no error...
but when I tried to run with emulator...
"Unfotunetly, ........... has stop"
please....help...me..
new ImageAdapter(this) Needs a constructor that takes context as the param.
Change this
public void ImageAdapater(Context c) { //remove void for constructor
to
public ImageAdapater(Context c) {
More info
http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
I created custom adapter for listview which contain text and images, on click of particular list item I have to open another activity, but I am not able to fire listview click event, below is my code.
Thanks.
Adapter
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
myCtx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
Main acitivity
public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.list_view_image);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.reg_item_names);
TypedArray icons = res.obtainTypedArray(R.array.reg_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
options, icons));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Respected Aafag#
You have write onclick of listview like below.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
System.out.println("working!!!!!!!!!");
}
Add listener
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
convertView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
return convertView;
}
I have a database which contains list of image and video path. My problem is I have to show all the images and video in a GridView. I have taken list of path from database but I am not able to show them in GridView. Please help me. Here is my code.
Thanks in Advance
public class GridGallery extends Activity
{
ArrayList<String>list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_gallery);
DataModel dbModel = new DataModel(this);
list = dbModel.selectAll();
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private final Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount()
{
return list.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
for(int i=0;i<list.size();i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(list.get(i));
picturesView.setImageBitmap(mBitmap);
}
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else
{
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
Yes..
I got the answer after a long experiment: here it is:
public class GridGallery extends Activity
{
ArrayList<String>list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_gallery);
DataModel dbModel = new DataModel(this);
list = dbModel.selectAll();
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private final Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount()
{
return list.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
if(list.get(position).contains(".jpg"))
{
bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
}
else if(list.get(position).contains(".mp4"))
{
bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
}
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else
{
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
This works fine for me
You have to just add this line before your return of the picturesView
Just add this
picturesView.setImageBitmap(bitmap);
just after this :
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
You will get it what you are willing to get!
Hope it'll help. I know I'm bit late but may be it'll be helpful someone else who is need of it.