how to store downloaded images to res\drawble folder

All your problems with Audio, Video and Images.

how to store downloaded images to res\drawble folder

Postby chiranjeevib » Mon Sep 07, 2009 7:46 am

hai......
i am a new learner of android. presently i am doing an android project about
weather forcasting . so in my application i want to store images which are coming from server into
res\drawble folder for display purpose.

so please , can anybody provide me the detailed steps or some help code
for this image storing.

thanking you in advance.......
chiranjeevib
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Aug 26, 2009 5:07 pm
Location: HYDERABAD

Top

Postby azraeal » Tue Sep 08, 2009 8:42 pm

The /res/drawable folder, or any folder in your project, is only for assets that are preloaded and static. At deployment, it is a binary and you cannot change your binary at runtime.

The proper method would be to download your files into your local application directory:
/data/data/<package name>/

Then you refer to your downloaded assets by filename. Once you have your images downloaded, grab them as bitmaps with
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap b = BitmapFactory.decodeFile(<pathName>);
  2.  
  3. Drawable asset = new BitmapDrawable(b);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Now you can use that drawable/bitmap to populate whatever it is you use to display your images.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

how to store downloaded images to res\drawble folder

Postby chiranjeevib » Wed Sep 09, 2009 4:36 pm

[/quote] hai azraeal
Thanks for your reply. ya i follwed your suggestion that storing image in data\data\pkg\files
and trying to retrive it to display on emulator. but i got only blank on emulator screen.
here is my java code.
[/syntax]public class Download extends Activity {
InputStream is=null;
FileOutputStream fos=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
int b;
fos=openFileOutput("image1.gif",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
Log.i("file status", "file created");
URL rurl = new URL("http://www.vaderradar.se/create_images.php?height=400&width=400&id=0");
URLConnection con= rurl.openConnection();
con.connect();
is=con.getInputStream();
while((b=is.read())!=-1)
{
osw.write(b);
}
Log.i("writing"," done");
is.close();
fos.close();
Bitmap bmp=BitmapFactory.decodeStream(openFileInput("image1.gif"));

//Bitmap bmp = BitmapFactory.decodeFile("data/data/proj.android.imagedownload/files");
Log.i("image","Bitmapped" );
Drawable asset = new BitmapDrawable(bmp);
ImageView iv=(ImageView)findViewById(R.id.ImageView01);
Log.i("image"," Image view creeated" );
iv.setImageDrawable(asset);
//iv.setImageBitmap(bmp);
Log.i("image","displayed" );

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[/syntax]

please go through my code and suggest me the right way ....
i need some code help.
like the above image display , i have to to display 4- 8 images as animation at runtime . please give some guidelines to complete this task ...

thanking you in advance
chiranjeevib
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Aug 26, 2009 5:07 pm
Location: HYDERABAD

Postby azraeal » Wed Sep 09, 2009 6:37 pm

For safety's sake, and to decrease confusion, I personally use the full path name and not assume anything with how the system would parse just a filename:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. fos=openFileOutput("data/data/proj.android.imagedownload/files/image1.gif",MODE_WORLD_READABLE);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

It may not matter but it serves to improve readability. Someone correct me if I'm wrong and it actually does place it under root.

Also, please go over the logcat output to see if you've caught any exceptions. Any please correct your post to have the proper formatting.

Since you have your file written (double check in DDMS on its filesize to confirm you've downloaded it all), why go back to using a stream? You already have the direct file access that you've commented out:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap bmp=BitmapFactory.decodeStream(openFileInput("image1.gif"));
  2.  
  3. //Bitmap bmp = BitmapFactory.decodeFile("data/data/proj.android.imagedownload/files");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Just note that you need to specify the full path of the file. So I'm assuming in your case, that would be
data/data/proj.android.imagedownload/files/image1.gif
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

how to store downloaded images to res\drawble folder

Postby chiranjeevib » Thu Sep 10, 2009 11:00 am

[quote]hai azraeal

Thank you for your reply.
yah , i followed your tips and its working now ,that displaying the image .
but in my project i want to display nearly 12 images in sequence as an animation effect.
can you give me suggestions.

first of all , i want to know is it working finely on real Android phone.
ie storing image files in data/data/[pakagename]/files/....is recommended or not.
is it avaliable in real Android phone or not.

Suggest me the recommended best approach ,
and give me some guideline tips or code help to get animation using gif images.

thanking you ...
chiranjeevib
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Aug 26, 2009 5:07 pm
Location: HYDERABAD

Postby azraeal » Thu Sep 10, 2009 5:36 pm

Yes, it is recommended to store your files under /data/data/<package>
No, you will not be able to see those files through a browser on the phone. But they are there.

As far as I know, Android does not natively support animated .gif files.

What you are seeking is a frame animation.

Look at http://developer.android.com/reference/ ... wable.html

Google's documentation has already covered this: http://developer.android.com/guide/topi ... phics.html
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Top

Postby pavelll73 » Wed Nov 18, 2009 7:30 pm

ImageView dlgImage = (ImageView)m_dialogContent.findViewById(R.id.icon);
InputStream is = null;
Bitmap bitmap = null;

try
{
URL url = new URL("http://www.cs.tut.fi/~karaoglu/SGN-3016/Documents/Baboon.bmp");
is = url.openStream();
int buff = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while((buff = is.read()) != -1)
{
outStream.write(buff);
}
byte[] bytes = outStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(bytes, 0 , bytes.length);
outStream.close();
is.close();
}
catch(Exception e) { }
dlgImage.setImageBitmap(bitmap);
azraeal wrote:Yes, it is recommended to store your files under /data/data/<package>
No, you will not be able to see those files through a browser on the phone. But they are there.

As far as I know, Android does not natively support animated .gif files.

What you are seeking is a frame animation.

Look at http://developer.android.com/reference/ ... wable.html

Google's documentation has already covered this: http://developer.android.com/guide/topi ... phics.html
pavelll73
Freshman
Freshman
 
Posts: 2
Joined: Fri May 08, 2009 1:12 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests