So I am making an app that has over 200 sunset pictures (taken by myself:P). I want to display them all via android.R.layout.simple_gallery_item I tried tossing them all in the drawable folder, then bitmapping each one and drawing it on the screen, but I get an Out of memory error.
What would be the best way to store these jpg?
The only two ways I can think of are:
A separate zip file download from the app?
Drawable File? (Using bitmap wouldn't be the right way?)
Somehow storing them in the apk then pushing them onto the device before installing the app?(If possible, because if I am correct(which I am probably not), when building the apk, everything comes together and some files morph into one)
(PS: If can't tell, I am new to Android Developing)
Thanks!
Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Noobs");
ArrayList<Image> img = new ArrayList<Image>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier("#drawable/bm" + String.valueOf(i), null, getPackageName());
img.add(ImageView.setImageResource(imageResource));
System.out.println("Did Pic "+String.valueOf(i));
}
System.out.println("Come so far!");
ListAdapter picAdapt = new ArrayAdapter<Image>(this, android.R.layout.simple_gallery_item, img);
ListView picListView = (ListView) findViewById(R.id.gallery);
picListView.setAdapter(picAdapt);
}
}
I recommend you use picasso http://square.github.io/picasso/
or glide https://github.com/bumptech/glide
those two help you to get and optimize your images
ArrayList<Integer> img = new ArrayList<Integer>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier(mContext.getPackageName() + ":drawable/bm" + String.valueOf(i), null, null);
img.add(imageResource);
}
ImageView imageView = findViewById(R.id.<UR_ID>);
imageView.setImageResource(img.get(0));
Related
I am developing an app that once a button is clicked I go to a new activity where 3 random images are set to an imageView. If the user does not like the 3 images selected, they can then click a button to generate 3 new random images. I am having two problems...
I have the imageView set to a specific size but when the images show up they are sometimes smaller and are showing horizontally. I would like them to show up vertically and all as the size of the imageView. How do I change the code to do this?
I am getting the new images to show up when I click the button but I am getting an error saying that I am running out of memory after I do it a few times. How do I change the code so I stop using all my memory?
Here is my code
public ImageView shirts;
public ImageView pants;
public ImageView shoes;
Button reload;
private Bitmap currentBitmap = null;
String[] projection = new String[]{
MediaStore.Images.Media.DATA,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.suggested_outfit);
shirts = (ImageView)findViewById(R.id.shirtImage);
pants = (ImageView)findViewById(R.id.pantsImage);
shoes = (ImageView)findViewById(R.id.shoesImage);
shirts.setImageBitmap(getImage());
pants.setImageBitmap(getImage());
shoes.setImageBitmap(getImage());
reload = (Button)findViewById(R.id.getNewOutfit);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
startActivity(getIntent());
}
});
}
public Bitmap getImage () {
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = getContentResolver().query(images,
projection,"", null, ""
);
final ArrayList<String> imagesPath = new ArrayList<String>();
if (cur.moveToFirst()) {
int dataColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATA);
do {
imagesPath.add(cur.getString(dataColumn));
} while (cur.moveToNext());
}
cur.close();
final Random random = new Random();
final int count = imagesPath.size();
int number = random.nextInt(count);
String path = imagesPath.get(number);
currentBitmap = BitmapFactory.decodeFile(path);
return currentBitmap;
}
}
I read that it isn't good practice to reload the activity but I can't figure out a way to generate 3 new random images. I am open for any suggestions if there is a better way to do this.
Obtaining images through an onClick method would have to have
do {
String path = cur.getString(dataColumn);
if(path.contains("yourDirectory")){ //in your case, your image directory
imagesPath.add(path);
}
} while (cur.moveToNext());
within the requested intent/activity
Decoding Bitmap consumes much memory, on your code you decode Bitmap many times when click button maybe causes OutOfMemory. Try to decode all your Bitmap once times and save them to a list. When a click to generate random Bitmap, you create random index of the list Bitmap and retrieve object by that index. Hope this help!
you can use this utils: image loader
it has memory manager.and you can set imageview when image load completed.
My app takes a photo using the camera. It then gets the URI, then the path of that photo and stores the path in a SQLite database. All of that seems to work fine, however when I go to view the images as bitmaps, this code only shows the same image (the last one) for every record (all others informations on each record displays correctly).
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mPhoto = BitmapFactory.decodeFile(timage.toString(), options);
viewHolder.img_image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.img_image.setImageBitmap(mPhoto);
This is where I get the information for timage, and the rest of the record details :
public ArrayList<ProductModel> _productlist = new ArrayList<ProductModel>();
ArrayList<ProductModel> product_list = db.getProducts();
for (int i = 0; i < product_list.size(); i++) {
String tidno = product_list.get(i).getIdno();
System.out.println("tidno>>>>>" + tidno);
timage = product_list.get(i).getImage();
System.out.println(timage);
String tname = product_list.get(i).getProductname();
String tdesc = product_list.get(i).getproductdesc();
ProductModel _ProductModel = new ProductModel();
_ProductModel.setIdno(tidno);
_ProductModel.setImage(timage);
_ProductModel.setProductname(tname);
_ProductModel.setproductdesc(tdesc);
_productlist.add(_ProductModel);
}
totalrecords.setText("Total Records :-" + _productlist.size());
listview.setAdapter(new ListAdapter(this));
db.close();
The first 3 lines are based on Vino's answer from Suggestions to avoid bitmap Out of Memory error - scaling the images to get rid of the out of memory errors I was receiving otherwise.
mPhoto is simply a Bitmap variable, timage is the images paths (which prints onto the console correctly).
Anyone able to give me a hint in the right direction to get the app showing every image, rather than just the last one taken ?
That variable timage is conserving the value of the last item in the for iteration.
for (int i = 0; i < product_list.size(); i++)
{
//...
timage = product_list.get(i).getImage();
//...
}
So after exiting the loop timage will have the value of the last item. And of course here
mPhoto = BitmapFactory.decodeFile(timage.toString(), options);
it's being used for all your images. Shouldn't you be using _ProductModel.getImage() instead?
i'm developing an app with in one activity a lot imageview. Sometimes when i try to start it this crash with an "outOfMemory" error.Here it is my activity code:
ImageView [] immagini = new ImageView[11];
String [] vett;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_techp);
Intent intent = getIntent();
//obtaining array from previous activity
vett=intent.getExtras().getStringArray("nomeImmagine");
//Instance ImageView
for (int i = 0; i < 11; i++){
int x=i+1;
immagini[i] = (ImageView) findViewById(getResources().getIdentifier("imageView"+x, "id", getPackageName()));
}
//setting image resource
for (int i = 0; i < vett.length; i++) immagini[i].setImageResource(getResources().getIdentifier(vett[i],"drawable", getPackageName()));
}
Any idea how can i resolve this?
You will need to cache your images, go here and see what Googles says about it. Always keep in mind that Android devices have a limited amount of memory, and may vary greatly according to each device.
Update
i know i can use array or list for it but i want to use an efficient way for it with out using arrays or collection thanks to all of you
special thanks to mussharapp
hi i want to access Imageview using its id
for example i have an imageview which is created using loop like
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
now i want to get all images which i have created in loop how i can get these images is there any way using id or some thing else ???
the same problem i am facing
declare a private arraylist:
private ArrayList<Imageview> images = new ArrayList<Imageview>();
then store all your views into that list:
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
images.add(mv);
}
Finally, you can get them based on position/id (since position == id):
ImageView imageView = images.get(position);
Remember to clear list when done or onDestroy().
images.clear();
Given that you are sure that the image view id's will be unique you can do something like this
//Your code
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
//...
List<Drawable> drawables = new ArrayList<Drawable>();
for (int i = 1; i < 10; i++) {
ImageView mv = (ImageView) findViewById(i);
drawables.add(mv.getDrawable());
}
Not sure what you are trying to do is efficient. You are probably better off with the other suggestions they have mentioned.
First you have to set Array of images with its path and then you can use it...
You can keep an array of ImageViews.
ImageView[] mImageViews = new ImageView[10];
for(int i=1;i<10;i++) {
mImageViews[i] = new ImageView(this);
}
And access these like mImageViews[2] or mImageViews[5]
I am using opensource OI ShoppingList template and trying to modify it from http://www.openintents.org/.
There are two Libraries and one main project, I am trying to move the libraries to main project which is "OI ShoppingList" but it gives me error in code.
I managed to move one of them without problems "ShoppingListLibrary". I had no problems moving DistributionLibrary, but when I move layout files ("oi_distribution_eula", "oi_distribution_infoactivity") to main project 3 source files broke.
Any ideas what I should change to make this work been searching everywhere but no luck.
Here is dropbox link to source.
https://www.dropbox.com/s/iisqncajbbd9gel/ShoppingList-source-1.6%20not%20working.rar
Thanks in advance
edit1: thanks for the feedback. Here is one of the codes the error is in setContentView(R.layout.oi_distribution_infoactivity);
under the layout the error reads "layout cannot be resolved or is not a field"
I have tried importing R class, cleaning the project but still i have that error
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDistribution.setFirst(MENU_DISTRIBUTION_START, DIALOG_DISTRIBUTION_START);
// Check whether EULA has been accepted
// or information about new version can be presented.
if (mDistribution.showEulaOrNewVersion()) {
return;
}
setContentView(R.layout.oi_distribution_infoactivity);
init();
mApplicationStrings = new String[mApplications.length];
for (int i = 0; i < mApplications.length; i++) {
mApplicationStrings[i] = getString(mApplications[i]);
}
setListAdapter(new FontArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mApplicationStrings));
ListView listview = getListView();
listview.setOnItemClickListener(this);
// Set message of activity
String appname = VersionUtils.getApplicationName(this);
String message = getString(R.string.oi_distribution_info_activity_text,
appname);
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(message);
/*
TypedArray a = obtainStyledAttributes(mTheme, R.styleable.ShoppingList);
String typefaceName = a.getString(R.styleable.ShoppingList_textTypeface);
mTextSizeMedium = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeMedium, 23);
mTextSizeLarge = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeLarge, 28);
mTextColor = a.getColor(R.styleable.ShoppingList_textColor, Color.BLACK);
Drawable background = a.getDrawable(R.styleable.ShoppingList_background);
View v = findViewById(R.id.background);
v.setBackgroundDrawable(background);
mTypeface = Typeface.createFromAsset(getResources().getAssets(), typefaceName);
TextView tv = (TextView) findViewById(R.id.text);
tv.setTypeface(mTypeface);
tv.setTextSize(mTextSizeMedium);
tv.setTextColor(mTextColor);
*/
}