Where can I store data files for my application?

Put your problem here if it does not fit any of the other categories.

Where can I store data files for my application?

Postby tum0rc0re » Fri Dec 21, 2007 7:24 am

I make minitool for set wallpaper for home screen. And I want that when I click on button "Add picture" will show list picture files from my current application path. Or can this pics store in another place? Now I use pics from drawable resources. A button "Set wallpaper" already work :) Help me.

Image

main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    android:background="#ff000000"
  7.    >
  8.     <TextView          
  9.         id="@+id/tvcurrentwallpaper"
  10.         android:textColor="#ffffffff"
  11.         android:textSize="12dip"
  12.                 android:layout_width="fill_parent"
  13.                 android:layout_height="wrap_content"
  14.                 android:textAlign="center"
  15.                 android:text="Current wallpaper"
  16.                 android:layout_marginTop="5dip"
  17.                 android:textStyle="bold"
  18.                 android:layout_marginBottom="5dip"
  19.                
  20.         />    
  21.     <ImageSwitcher
  22.         id="@+id/switcher"
  23.        android:layout_width="fill_parent"
  24.        android:layout_height="fill_parent"                
  25.        android:layout_alignParentLeft="true"
  26.                 android:layout_alignParentRight="true"
  27.                 android:layout_alignParentBottom="true"        
  28.                 android:layout_below="@id/tvcurrentwallpaper"
  29.    />
  30.     <Gallery id="@+id/gallery"
  31.        android:background="#55000000"
  32.        android:layout_width="fill_parent"
  33.        android:layout_height="60dip"        
  34.        android:layout_alignParentLeft="true"
  35.                 android:layout_alignParentRight="true"
  36.        android:gravity="center_vertical"              
  37.        android:spacing="16"
  38.        android:layout_above="@+id/btnsetwallpaper"
  39.    />
  40.     <Button
  41.         id="@+id/btnsetwallpaper"      
  42.         android:text="Set as wallpaper"
  43.         android:textSize="12dip"
  44.         android:layout_width="wrap_content"
  45.                 android:layout_height="wrap_content"
  46.                 android:layout_alignParentBottom="true"
  47.                 android:layout_alignParentLeft="true"
  48.                 android:textAlign="center"             
  49.    />
  50.     <Button
  51.         id="@+id/btnaddpicture"        
  52.         android:text="Add picture"
  53.         android:textSize="12dip"
  54.         android:layout_width="wrap_content"
  55.                 android:layout_height="wrap_content"
  56.                 android:layout_alignParentBottom="true"
  57.                 android:layout_alignParentRight="true"
  58.                 android:textAlign="center"                     
  59.    />
  60. </RelativeLayout>
  61.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4

WallpaperSwitcher.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package maximyudin.wallpaperswitcher;
  2.  
  3. import java.util.ArrayList;
  4. import android.app.*;
  5. import android.content.*;
  6. import android.os.Bundle;
  7. import android.view.*;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.*;
  10. import android.widget.Gallery.LayoutParams;
  11. import android.graphics.Bitmap;
  12. import android.graphics.Canvas;
  13. import android.graphics.drawable.Drawable;
  14.  
  15. public class WallpaperSwitcher extends Activity implements
  16. ViewSwitcher.ViewFactory, AdapterView.OnItemSelectedListener {
  17.         /** Called when the activity is first created. */
  18.         private ImageSwitcher mISwitcher;      
  19.         private int mCurrentSelectedImage;
  20.         private TextView mTVCurrentWallpaper;  
  21.         private ArrayList<Drawable> allimages = new ArrayList<Drawable>();     
  22.  
  23.         @Override
  24.         public void onCreate(Bundle icicle) {
  25.                 super.onCreate(icicle);      
  26.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  27.                 setContentView(R.layout.main);                                                         
  28.  
  29.                 for (int i = 0; i < mImageIds.length; i++) {                   
  30.                         allimages.add(this.getResources().getDrawable(mImageIds[i]));
  31.                 }
  32.  
  33.                 mISwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  34.                 mISwitcher.setFactory(this);
  35.                 mISwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  36.                                 android.R.anim.fade_in));
  37.                 mISwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  38.                                 android.R.anim.fade_out));
  39.  
  40.                 Gallery gallery = (Gallery) findViewById(R.id.gallery);
  41.                 gallery.setAdapter(new ImageAdapter(this));
  42.                 gallery.setSelectorSkin(getResources().getDrawable(android.R.drawable.box));
  43.                 gallery.setOnItemSelectedListener(this);    
  44.  
  45.                 final Button btnSetWallpaper = (Button) findViewById(R.id.btnsetwallpaper);
  46.                 btnSetWallpaper.setOnClickListener(btnSetWallpaperListener);  
  47.                 mTVCurrentWallpaper = (TextView) findViewById(R.id.tvcurrentwallpaper);
  48.         }
  49.  
  50.        
  51.         private Button.OnClickListener btnSetWallpaperListener = new Button.OnClickListener()
  52.         {
  53.                 public void onClick(View v)
  54.                 {
  55.                         NotificationManager nm = (NotificationManager)
  56.                         getSystemService(NOTIFICATION_SERVICE);                
  57.                         try {                                                                                                  
  58.                                 Bitmap newwallpaper = Bitmap.createBitmap(320, 240, true);
  59.                                 Canvas myCanvas = new Canvas(newwallpaper);                    
  60.                                 allimages.get(mCurrentSelectedImage).draw(myCanvas);
  61.                                 setWallpaper(newwallpaper);                            
  62.                                 broadcastIntent(new Intent (Intent.WALLPAPER_CHANGED_ACTION));                                                                 
  63.  
  64.                                 nm.notifyWithText(R.id.btnsetwallpaper,
  65.                                                 getText(R.string.newwallpaperset),
  66.                                                 NotificationManager.LENGTH_SHORT, null);
  67.  
  68.                         }
  69.                         catch (Exception e) {
  70.                                 nm.notifyWithText(R.id.btnsetwallpaper,
  71.                                                 getText(R.string.newwallpaperfailed),
  72.                                                 NotificationManager.LENGTH_SHORT, null);
  73.                         }
  74.                 }
  75.         };
  76.  
  77.         public void onItemSelected(AdapterView parent, View v, int position, long id) {
  78.                 mCurrentSelectedImage = position;        
  79.                 mISwitcher.setImageDrawable(allimages.get(position));
  80.                 mTVCurrentWallpaper.setText(getText(R.string.selectedpicture) + " [ "
  81.                                 + (mCurrentSelectedImage + 1) + "/" + allimages.size()                         
  82.                         + " ]");
  83.         }
  84.  
  85.         public void onNothingSelected(AdapterView parent) {
  86.         }
  87.  
  88.         public View makeView() {
  89.                 ImageView i = new ImageView(this);
  90.                 i.setBackgroundColor(0xFF000000);
  91.                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  92.                 i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  93.                                 LayoutParams.FILL_PARENT));
  94.                 return i;
  95.         }
  96.  
  97.         public class ImageAdapter extends BaseAdapter {
  98.                 private Context mContext;
  99.  
  100.                 public ImageAdapter(Context c) {
  101.                         mContext = c;
  102.                 }
  103.  
  104.                 public int getCount() {
  105.                         return allimages.size();
  106.                 }
  107.  
  108.                 public Object getItem(int position) {
  109.                         return position;
  110.                 }
  111.  
  112.                 public long getItemId(int position) {
  113.                         return position;
  114.                 }
  115.  
  116.                 public View getView(int position, View convertView, ViewGroup parent) {            
  117.                         ImageView galleryview = new ImageView(mContext);
  118.                         galleryview.setImageDrawable(allimages.get(position));
  119.                         galleryview.setAdjustViewBounds(true);
  120.                         galleryview.setLayoutParams(new Gallery.LayoutParams(
  121.                                         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  122.                         galleryview.setBackground(android.R.drawable.picture_frame);            
  123.                         return galleryview;
  124.                 }
  125.  
  126.                 public float getAlpha(boolean focused, int offset) {
  127.                         return Math.max(0.2f, 1.0f - (0.2f * Math.abs(offset)));
  128.                 }
  129.  
  130.                 public float getScale(boolean focused, int offset) {
  131.                         return Math.max(0, offset == 0 ? 1.0f : 0.6f);
  132.                 }      
  133.         }
  134.  
  135.         private Integer[] mImageIds = {
  136.                         R.drawable.sample_0, R.drawable.sample_1};
  137. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Top

Postby Scionwest » Tue Dec 25, 2007 6:13 am

I like the layout of the application. Sorry I couldn't help any.
Scionwest
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 25, 2007 6:04 am

Postby tum0rc0re » Tue Dec 25, 2007 6:23 am

Scionwest wrote:I like the layout of the application. Sorry I couldn't help any.

Thanx, I hope I will make this mini app, when I finish examinations :) Now I have no time :( Too I want to add feature for change wallpaper by timer, e.g. every 15 min it will be change wallpaper from set of pictures :)
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby Asgard » Tue Dec 25, 2007 8:48 pm

Try this folder "data/data/your_package_name/files/".
Catch the moment
Asgard
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sat Dec 15, 2007 8:49 pm

Postby tum0rc0re » Tue Dec 25, 2007 9:30 pm

Asgard wrote:Try this folder "data/data/your_package_name/files/".


Thanks, I simply think that the folder "data/data/your_package_name" is temp, or not?
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby Asgard » Wed Dec 26, 2007 8:42 am

No, files in this folder are alive until your application is installed at emulator. Just try.
Catch the moment
Asgard
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sat Dec 15, 2007 8:49 pm

Top

Postby tum0rc0re » Wed Dec 26, 2007 8:45 am

Asgard wrote:No, files in this folder are alive until your application is installed at emulator. Just try.


Thanks once more :) I'll try :)
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Tutorial ImageSwitcher

Postby wildstream » Sat Dec 29, 2007 8:59 am

I hadn't notice it existed this tutorial.

Thank you very Much, :P !!!
wildstream
Freshman
Freshman
 
Posts: 2
Joined: Fri Dec 28, 2007 6:55 pm

Re: Tutorial ImageSwitcher

Postby tum0rc0re » Sat Dec 29, 2007 9:08 am

wildstream wrote:I hadn't notice it existed this tutorial.

Thank you very Much, :P !!!


Not at all :)
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby keiko11245 » Fri Jun 05, 2009 4:47 pm

Hi,
I'm trying to do something like that (change the home screen wallpaper) with the 1.5 SDK and it doesn't work.


I have an Activity with this code :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onCreate(Bundle savedInstanceState) {
  3.  
  4.     super.onCreate(savedInstanceState);
  5.  
  6.  
  7.  
  8.     setContentView(R.layout.layout_background_change);
  9.  
  10.  
  11.  
  12.     Button b = (Button) findViewById(R.id.changement_bt);
  13.  
  14.     i = (ImageView) findViewById(R.id.image_bg);
  15.  
  16.  
  17.  
  18.     b.setOnClickListener(this);
  19.  
  20. }
  21.  
  22.  
  23.  
  24. @Override
  25.  
  26. public void onClick(View view) {
  27.  
  28.     try {
  29.  
  30.         getImage("http://.../image1.png","/sdcard/images/","image_bg.png");
  31.  
  32.         Bitmap b = BitmapFactory.decodeFile("/sdcard/images/image_bg.png");
  33.  
  34.         i.setImageBitmap(b);
  35.  
  36.                        
  37.  
  38.         getApplication().setWallpaper(b);
  39.  
  40.         sendBroadcast(new Intent(Intent.ACTION_WALLPAPER_CHANGED));
  41.  
  42.     } catch (Exception e) {
  43.  
  44.         Log.e("BackgroundChange", "background image download error", e);
  45.  
  46.     }
  47.  
  48. }
  49.  
  50.  
  51.  
  52. private void getImage(String pathSource, String directory, String name) throws IOException {
  53.  
  54.     URL url = new URL(pathSource);
  55.  
  56.     URLConnection cn = url.openConnection();
  57.  
  58.     cn.connect();
  59.  
  60.  
  61.  
  62.     InputStream stream = cn.getInputStream();
  63.  
  64.     int contentLength = cn.getContentLength();
  65.  
  66.     if (stream == null)
  67.  
  68.         throw new RuntimeException("stream is null");
  69.  
  70.  
  71.  
  72.     InputStream in = new BufferedInputStream(stream);
  73.  
  74.     byte[] data = new byte[contentLength];
  75.  
  76.  
  77.  
  78.     int bytesRead = 0;
  79.  
  80.     int offset = 0;
  81.  
  82.     while (offset < contentLength) {
  83.  
  84.         bytesRead = in.read(data, offset, data.length - offset);
  85.  
  86.         if (bytesRead == -1)
  87.  
  88.             break;
  89.  
  90.         offset += bytesRead;
  91.  
  92.     }
  93.  
  94.     in.close();
  95.  
  96.  
  97.  
  98.     if (offset != contentLength) {
  99.  
  100.         throw new IOException("Only read " + offset + " bytes; Expected "+ contentLength + " bytes");
  101.  
  102.     }
  103.  
  104.  
  105.  
  106.     String filename = directory + name;
  107.  
  108.     File f = new File(directory);
  109.  
  110.     if (f.exists() == false) {
  111.  
  112.         f.mkdir();
  113.  
  114.     }
  115.  
  116.  
  117.  
  118.     FileOutputStream out = new FileOutputStream(filename);
  119.  
  120.     out.write(data);
  121.  
  122.     out.flush();
  123.  
  124.     out.close();
  125.  
  126.  
  127.  
  128.     cn = null;
  129.  
  130. }
  131.  
  132.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


When I click on my button, the home screen wallpaper doesn't change.
My image is in the /sdcard/images directory.

Is it the good function for doing this ?
Do my application need some specific permission ?
keiko11245
Freshman
Freshman
 
Posts: 7
Joined: Tue Mar 10, 2009 11:49 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests