i have designed an app to show images in a view Pager, now the thing is that user saves this images to its internal memory and the app gives a random no. to the image as name, upon clicking "View Favorite " button the user gets to view all the images in a view pager one by one, can any1 help me as to how i go about it??
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "image.png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
Context context =getApplicationContext();
final ImageView imageView = new ImageView(getApplicationContext());
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
this is the code to load the image but the thing is i need to load all the images not just one image.png but everything.png ;) how do i do it??
and also i wanna load with Picasso but i cant load the bitmap it says something like load is not for bitmap etc. etc. please help
I don't know if you have already solved this issue. But if you don't give your files a random number when you save them, you could give them sequential numbers like 1.jpg, 2.jpg .... then you could do a while loop to load each image. For example:
int counter = 0;
boolean imageExists = true;
while(imageExists)
{
File imageFile = new File (filePath + counter + ".jpg");
if(imageFile.exists())
{
Picasso.with(getBaseContext()).load(imgFile).fit().centerInside().into(imageView);
}
else
{
imageExists = false;
}
}
Hopefully this helps.
Related
Is it possible to show previously downloaded image in Glide as placeholder while downloading new image.
Like I have an image loaded in imageview using glide. Now the imageurl is changed, so while loading this new image is it possible to keep displaying the old image (might be from cache).
What I want is while the new image is being loaded from the URL, is it possible to keep the current image as placeholder.
I found the answer to this in the discussion here - https://github.com/bumptech/glide/issues/527#issuecomment-148840717.
Intuitively I also thought of using placeholder(), but the problem is that as soon as you load the second image, you loose the reference to the first one. You can still reference it but it is not safe as it may be reused by Glide or recycled.
The proposed solution from the discussion is to use thumbnail() and load the first image again. The load will return the first image immediately from the memory cache and it will look as if the image did not change until the second image is loaded:
String currentImageUrl = ...;
String newImageUrl = ...;
Glide.with(this)
.load(newImageUrl)
.thumbnail(Glide.with(this)
.load(currentImageUrl)
.fitCenter()
)
.fitCenter()
.into(imageView);
Glide have a capability of getting the bitmap of the image from that url, so just get it and then save it to a desired storage into your phone, and after that in your .placeholder() just use that bitmap when you are trying to get another image , take a look at this snippet
/** Download the image using Glide **/
Bitmap theBitmap = null;
theBitmap = Glide.
with(YourActivity.this).
asBitmap().
load("Url of your image").
into(-1, -1).
get(); //with this we get the bitmap of that url
saveToInternalStorage(theBitmap, getApplicationContext(), "your preferred image name");
/** Save it on your device **/
public String saveToInternalStorage(Bitmap bitmapImage, Context context, String name){
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/imageDir
String name_="foldername"; //Folder name in device android/data/
File directory = cw.getDir(name, Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,name_);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("absolutepath ", directory.getAbsolutePath());
return directory.getAbsolutePath();
}
/** Method to retrieve image from your device **/
public Bitmap loadImageFromStorage(String path, String name)
{
Bitmap b;
String name_= name; //your folderName
try {
File f=new File(path, name_);
b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
/** Retrieve your image from device and set to imageview **/
//Provide your image path and name of the image your previously used.
Bitmap b= loadImageFromStorage(String path, String name)
ImageView img=(ImageView)findViewById(R.id.your_image_id);
img.setImageBitmap(b);
I have relativelayout (A template) where it contain textboxes whose content is populated at runtime.On clicking save button,I need to save the template as an image in SD card.Is it possible.
I referred the below link:
How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?
But the image saved cannot be opened.
Is my requiremnet possible.Or else please advice how can I achieve it.
I am behind this for several days.I am new to ANdroid.Please help.
Thanks in Advance.
You can pass any view or layout to devBitmapFrmViewFnc function and get the bitmap. You can save the bitmap in jpeg using devImjFylFnc.
|==| Dev Bitmap Image from View :
Bitmap devBitmapFrmViewFnc(View viewVar)
{
viewVar.setDrawingCacheEnabled(true);
viewVar.buildDrawingCache();
return viewVar.getDrawingCache();
}
|==| Create a JPG File from Bitmap :
static void devImjFylFnc(String MobUrlVar, Bitmap BitmapVar)
{
try
{
FileOutputStream FylVar = new FileOutputStream(MobUrlVar);
BitmapVar.compress(Bitmap.CompressFormat.JPEG, 100, FylVar);
FylVar.close();
}
catch (Exception ErrVar) { ErrVar.printStackTrace(); }
}
I am trying to load the thumbnails for videos in a file browser but its causing me to run out of memory.
public class Filea {
private Bitmap VideoIcon;
public Bitmap getVideoIcon() {
return VideoIcon;
}
public void setVideoIcon(Bitmap videoIcon) {
VideoIcon = videoIcon;
}
This is being done for every video file that is in a folder.
Every time that I load a new folder does it keep the information from the previous folder, and if so how do i get it to delete the unwanted resources?
private List<Filea> LoadFiles(String dirPath) {
inSearch = false;
List<Filea> files = new ArrayList<Filea>();
try {
for(String F:EditedFileList(current)) {
Filea file = new Filea();
file.setVideoIcon(ThumbnailUtils.createVideoThumbnail(current + "/" + getName(F), MediaStore.Images.Thumbnails.MINI_KIND));
files.add(file);
}
} catch (NullPointerException e) {
e.getStackTrace();
} catch (NoSuchElementException e) {
Log.i("LoadFiles", "No files found");
}
return files;
}
This is how the information is obtained.
Along with the video thumbnails I am loading other bits of data E.g. File name, size, permissions, Image icons(as bitmaps) ect..
Most likely you're keeping around a bunch of unneeded references. My suggestion would be to put the images in an LRUCache and load them from there. That way you can set a reasonable maximum amount of memory to use on images.
I found the problem, i was trying to load the full image of picture instead of the thumbnails.`
file.setImageIcon(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(current + "/" + getName(F)), 100, 100));
Thank. LukeMovement.
I have images on the SD card and I want to show these images in an Activity with a ViewPager.
On horizontal scroll the image should change.
With this methode you can retrive your picture from SDcard in a drwable object
public Drawable getImageFromSdCard(String imageName) {
Drawable d = null;
try {
String path = Environment.getExternalStorageDirectory().toString()
+ "/YourSubDirectory/";
Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
+ ".png");
d = new BitmapDrawable(bitmap);
} catch (IllegalArgumentException e) {
// TODO: handle exception
}
return d;
}
and then just call this Drawable in your ViewPager.
G.Luck
use this: Retrieving only images from gallery when firing android image chooser intent to get image from gallery.
And use this to show it horizontally: http://developer.android.com/resources/tutorials/views/hello-gallery.html
You need to club both these codes together.
I am trying to use an image from the sd card and set it as the background for a relativelayout. I have tried other solutions that i have found here and elsewhere but they havent seemed to work for me. here is my code. I have commented out other ways that i have tried and didnt work. the only thing that worked for me was using setBackgroudnResource and using a resource from the app, but this was just to test to make sure mRoot was set up correctly. when I have tried all the other ways, it just doesn't set anything. Anyone know what I am doing wrong, or if there is a better way to do this?
//one way i tired...
//String extDir = Environment.getExternalStorageDirectory().toString();
//Drawable d = Drawable.createFromPath(extDir + "/pic.png");
//mRoot.setBackgroundDrawable(d);
//another way tried..
//Drawable d = Drawable.createFromPath("/sdcard/pic.png");
//mRoot.setBackgroundDrawable(d);
//last way i tried...
mRoot.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
//worked, only to verify mRoot was setup correctly and it could be changed
//mRoot.setBackgroundResource(R.drawable.bkg);
You do not load a drawable from SD card but a bitmap. Here is a method to load it with the reduced sampling (quality) so the program will not complain if the image is too large. Then I guess you need to process this bitmap i.e. crop it and resize for the background.
// Read bitmap from Uri
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce quality
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
The Uri here can be supplied from a gallery picker activity.
The image then can be saved into application resources and loaded into an imageView
private void saveBackground(Bitmap Background) {
String strBackgroundFilename = "background_custom.jpg";
try {
Background.compress(CompressFormat.JPEG, 80, openFileOutput(strBackgroundFilename, MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG, "Background compression and save failed.", e);
}
Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(BackgroundSettings.this.getFilesDir(), strBackgroundFilename));
// Load this image
Bitmap bitmapImage = BitmapFactory.decodeFile(imageUriToSaveCameraImageTo.getPath());
Drawable bgrImage = new BitmapDrawable(bitmapImage);
//show it in a view
ImageView backgroundView = (ImageView) findViewById(R.id.BackgroundImageView);
backgroundView.setImageURI(null);
backgroundView.setImageDrawable(bgrImage);
}
File file = new File( url.getAbsolutePath(), imageUrl);
if (file.exists()) {
mDrawable = Drawable.createFromPath(file.getAbsolutePath());
}
I suggest checking that the drawable is being loaded correctly. Some things to try:
Try using a different image on the sd card
Put pic.png in R.drawable and make sure mRoot.setBackgroundResource() does what you expect
After loading the drawable, check d.getBounds() to make sure it is what you expect