Save images in external storage from Parse.com - android

I want to download images from parse database table and store all the images in a external memory folder. Is it possible ? If yes , how ??

Yes of course it is possible. But you need to follow some steps:
-Start downloading the image Url from your Parse object:
String url=yourParseObject.getParseFile("Image").getUrl();
-Now pass that url to this function:
public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
final ImageView imageView = (ImageView) rowView.findViewById(R.id.image);//Your imageView in the layout
AsyncImageLoader async=new AsyncImageLoader();
Drawable cachedImage = async.loadDrawable(
url, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable,
String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
imageView.setImageDrawable(cachedImage);
return cachedImage;
}
-If you want to save that in SD Card instead of ImageView, export that inside of the method (Drawable imageDrawable,String imageUrl) in drawable or bitmap form.
Hope it helps!

you can do that just you need to write code for download image from url and for url you can get using ParseFile.getUrl() and for downloading file you can check this tutorial
http://javatechig.com/android/download-image-using-asynctask-in-android

You can get download image from parse.com in bitmap then store this bitmap as image in your file directory as you want.
Refer this
android-parse-com-image-download-tutorial

Related

Download images and save it

I'm working on a school android project.
I need to have a download button which downloads a picture(when we have class)
And after display it in another activity(even in offline mode, and after quiting)
I've tried picasso, but I can't get it to save and use it in offline mode.
For you to support offline mode, You need to Save the image on your disk because when your cache is cleared, The image is cleared as well.
You can easily use Glide to Solve this, also storing on device and retrieving
You can Learn more about Glide here http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
/** Download the image using Glide **/
Bitmap theBitmap = null;
theBitmap = Glide.
with(YourActivity.this).
load("Url of your image").
asBitmap().
into(-1, -1).
get();
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_="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);
Thanks to #Droidman :
How to download and save an image in Android
Of course you can perform downloading and managing images by yourself,
but if your project is quite complex already, there are a lot of
libraries around and you do not need to reinvent the wheel. I won't
post code this time since there are a lot of examples, but I'm going
to tell you about 2 most useful libraries (IMO) related to image
downloading.
1) Android Volley. A powerful networking library created by Google and
covered by official documentation. POST'ing or GET'ing data, images,
JSON - volley will manage it for you. Using volley just for image
downloading is a bit of an overkill in my opinion.
2) Picasso
Image downloading and caching, perfect for
ListView/GridView/RecyclerView. Apache 2.0 license.
3) Fresco
Quite a new image loading library created by Facebook. Progressive
JPEG streaming, gifs and more. Apache 2.0
You could use Android Library called Universal Image Loader:
https://github.com/nostra13/Android-Universal-Image-Loader

Universal Image Loader Load bitmap from memory

Universal Image Loader provide many ways to load the image.
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
But all these way load image form file, I need a way to load from memory since my images was encrypted and stored in the assets folder, When I display this image, I need the following steps.
decrypt the image into bytes array.
Create bitmap from the bytes.
Load/display the image.
So it's something like this. Is that possible?
Bitmap bitmap = decrypt(encryptedImageFile);
imageLoader.displayImage(bitmap, imageView);
Currently, I am considering to save the bitmap to file and load the file, but this will take more time.
I believe the below is what you are seeking if your images are stored in image folder in assets directory, then you can get the list of images
private List<String> getImage(Context conetx) throws IOException {
AssetManager assetManager =conetx.getAssets();
String[] files = assetManager.list("image");
List<String> it=Arrays.asList(files);
return it;
}
As a note, instead of using assets dir, put the file into /res/raw and you can then access it using the following URI
android.resource://com.your.packagename/" + R.raw.<nameoffile>
I think you need to understand this. You must know, If you have read the source code of universal-image-loader, the order of loading a image into a ImageView after the image's url is provided, is: memory, SDCard(if set), internet. That means after you called, ImageLoader.display(url, imageview);, it will look for the Bitmap from memory first, if it doesn't exist, if will look for the file of the image from SDCard then, if the file exist, if will convert the file into a Bitmap, then load the Bitmap into the ImageView and store it in memory. But if the file doesn't exist, it will download the image file of the url, then store the file into the SDCard and convert the file into a Bitmap and load the Bitmap into memory. Most importantly, I recommend you to read source codes of it, if you are confused with what I post above.
So, it is unnecessary for you to load the Bitmap from memory, ImageLoader will do it for you.
Lets choose own scheme so our URIs will look like "stream://...".
Then implement ImageDownloader. We should catch URIs with our scheme and return image stream.
public class StreamImageDownloader extends BaseImageDownloader {
private static final String SCHEME_STREAM = "stream";
private static final String STREAM_URI_PREFIX = SCHEME_STREAM + "://";
public StreamImageDownloader(Context context) {
super(context);
}
#Override
protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
if (imageUri.startsWith(STREAM_URI_PREFIX)) {
return (InputStream) extra;
} else {
return super.getStreamFromOtherSource(imageUri, extra);
}
}
}
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.imageDownloader(new StreamImageDownloader(getApplicationContext()))
.build();
ImageLoader.getInstance().init(config);
ByteArrayInputStream stream = new ByteArrayInputStream(imgBytes);
String imageId = "stream://" + stream.hashCode();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.extraForDownloader(stream)
.build();
ImageLoader.getInstance().displayImage(imageId, imageView, options);

Can't see image in ImageView android

I am trying to populate ListView Item with an object from MyClass. One of the property of the class is jpg image. I put my images in images/ folder. I use this code for populating
private static final String ASSETS_DIR = "images/";
String imgFilePath=ASSETS_DIR+r.resourceID;
try{
Bitmap bitmap = BitmapFactory.decodeFile(imgFilePath);
resourceIdView.setImageBitmap(bitmap);
}
catch(Exception e)
{
System.out.println(" Error");
}
r.resourceID is the name of the image for example "AUD.jpg"
resourceIDView is ImageView
The program don't get in the catch part, however I can't see the image
could somebody help me??
From your naming convention I conclude that you are storing your images in the "assets" folder. If yes, then you can use the following lines of code and get this issue resolved:
private static final String ASSETS_DIR = "images/";
String imgFilePath=ASSETS_DIR+r.resourceID;
try{
Drawable d = Drawable.createFromStream(getAssets().open(imgFilePath), null);
resourceIdView.setImageDrawable(d);
}
catch(Exception e)
{
System.out.println(" Error");
}
Hope this helps.
put your image in drawable folder and set so imageview...
resourceIdView.setImageResource(R.drawable.AUD);
You can try to put your images in drawable folder under /res. Use an ImageAdapterthat extend BaseAdapterto populate your ListView. You can use this code : http://www.java2s.com/Code/Android/2D-Graphics/extendsBaseAdaptertocreateImageadapter.htm
What I had was that the image was showing in Designer but not on device, so I put the image in all the drawable-xdpi directories. This worked for me.

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

How do I get the initial url of an ImageView formed from a Bitmap

Initially I created a bitmap from an external URL by using a bitmapFactory. I then put the bitmap in an Image view, like:
ImageView mim = (ImageView) findViewById(R.id.moviepix);
String mi = "http://xxxxxxx.com/"+ result.movie_pix;
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(mi).getContent());
mim.setImageBitmap(bitmap);
Now I need to reverse engineer the url from the ImageView.
All help is appreciated.
You can't, but you can however store the URL for later use with ImageView.setTag()

Categories

Resources