0

I am create an directory to put audio files created by my app. The directory is on the SD card.

But if people modify that folder on the SD card manually then how can my app detect that event and rename the path to save the audios in the app? Or if the folder is deleted can it be detected too?

And another question:

Assume that the audio files created in a special folder on the SD card are listed to a ListView. (Application.onCreate automatically lists all audio files created by the application that exist in that special folder).

Now we exit from the app and go to that folder and copy another audio not created by my app there. Now we run the app again. That copied audio mustn't show as a created audio file because it doesn't actually belong my program. Is that possible?

how understands my app that an copied audio file from other place to folder that belongs with my program is not belong it surely...

this idea i was that every time an audio created then listing all audio on my folder on sdcard again. but this some problem,because maybe people chagne or modify(such as copy other audios to my folder or rename that or modify one of audios by cut or merge) now app how understand this event? your help will be appreciate...

UPDATE: I read this link and found this: Using Shared Preferences

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

User Preferences

Shared preferences are not strictly for saving "user preferences," such as what ringtone a user has chosen. If you're interested in creating user preferences for your application, see PreferenceActivity, which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).

To get a SharedPreferences object for your application, use one of two methods: •getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. •getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

To write values: 1.Call edit() to get a SharedPreferences.Editor.
2.Add values with methods such as putBoolean() and putString().
3.Commit the new values with commit()
To read values, use SharedPreferences methods such as getBoolean() and getString().

Here is an example that saves a preference for silent keypress mode in a calculator:

 public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}

and read this link adviced me before. How can I refresh MediaStore on Android?

but i cant use these informations. i need more.

Edit 2: Hi. I dont think saving my objects in SharedPrefrences solves my problem. becuse when an audio that created by my app make changes(name changing for example) then when app runs,that dosnt know variations.and i suppose my app cant perform these variations in my list yet. isnt true?? I write this code but when I record an audio and save its name with prefrences and i exit from app and i change my audio,s name and run my program again my audio,s name must be change in textview.but show "null" just. i think using of sharedPrefrences dosnt solve my issue.

` import android.app.Activity;

import android.content.SharedPreferences; import android.os.Bundle;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class TestSharedActivity extends Activity {

 Button btnst=null;

   Button btnsp=null;

    AudioRecorder myAudio=null;

    TextView tv=null;

    String audioName=null;


public static final String PREFS_NAME = "MyPrefsFile";


protected void onCreate(Bundle state)
{
   super.onCreate(state);
   setContentView(R.layout.main);

   btnst=(Button) findViewById(R.id.btnrec);
   btnsp=(Button) findViewById(R.id.btnstop);
   tv=(TextView) findViewById(R.id.textv);

   onTouchListeners();

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  // boolean silent = settings.getBoolean("silentMode", false);
   String name = settings.getString("mname","null");
   setName(name);
}

private void setName(String name)
{
    tv.setText(name);

}

@Override
protected void onStop()
{
   super.onStop();

  // We need an Editor object to make preference changes.
  // All objects are from android.context.Context
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();

  editor.putString("mname", audioName);

  // Commit the edits!
  editor.commit();
}
/////////////////////////
private void onTouchListeners()
 {

         View.OnTouchListener startListener=new View.OnTouchListener() 
         {

            public boolean onTouch(View v, MotionEvent event) 
            {
                if (MotionEvent.ACTION_DOWN == event.getAction()) 
                {           

                }
                else if (MotionEvent.ACTION_UP == event.getAction()) 
                {       
                    myAudio=new AudioRecorder();
                    audioName=myAudio.getName();
                    String path=myAudio.getPath();
                    tv.setText(path);

                    try
                    {

                      myAudio.Record();
                    }
                    catch(Exception e)
                    {
                        Log.d("audio recorddddddddd","cant starttt!!!!!!!!!!!!");
                    }

                }
                return true;
            }
         }; 

        ////////////////////////////////////////////////
         View.OnTouchListener stopListener=new View.OnTouchListener() 
         {

            public boolean onTouch(View v, MotionEvent event) 
            {
                if (MotionEvent.ACTION_DOWN == event.getAction()) 
                {               

                }
                else if (MotionEvent.ACTION_UP == event.getAction()) 
                {


                try
                    {
                      myAudio.stopRecord();
                    }
                    catch(Exception e)
                    {
                        Log.d("audio record stoppppppppppp",e.toString());
                    }

                }
                return true;
            }
         }; 

          btnst.setOnTouchListener(startListener);
          btnsp.setOnTouchListener(stopListener);
 }//end ontouch

} ` AudioRecorder is an class for record and save audios to my special folder on SDcard. and main.xml have 2 button "record and stop" and a textview for show name my audio and when run again it shows changed audio,s name.(if it take changes)

Community
  • 1
  • 1
setare shojaei
  • 145
  • 1
  • 12

2 Answers2

0

You have to change your logic a bit, I assume that when you create an audio you simply write it to SD card. In order to distinguish your sounds from someone's else ( in that folder ), you have to maintain a list of created sounds somewhere

You can do that in various ways:

  1. SharedPreferences
  2. SQLlite database
  3. Writing to additional text file in sd card in plain text (you can also encrypt it, shift bytes - do something so it would become unreadable by text editor or other person )

Hope this helps

edit:

An easy way would be to populate a list with your objects ( with all the meta data that you need) and serialize it to a private file(or string entry in shared preferences). This way you can have quite a lot of metadata, although you might have issues when object structure is changed in the future(of course if it gets changed).

See my other answer on how you can do it: What is the most suitable way to store object in android?

Community
  • 1
  • 1
FDIM
  • 1,981
  • 19
  • 21
  • exceuse me! whats means SharedPrefrences?? too, i have an list(this worked such a database.isnt true?? i dont want people cant modify audios.i want later people can use audios.maybe they will wanted use or got them from sdcard to another device – setare shojaei Oct 23 '13 at 07:43
  • @setareshojaei See this: http://developer.android.com/reference/android/content/SharedPreferences.html, You can get instance from activity via getPreferences() method. Its a simple way to persist settings or small amount of data. – FDIM Oct 25 '13 at 19:28
  • Hi FDIM. thank u.but read this link. i dont know how save my shared data (each audios with their info such as name,size,modify date,...) yet.what do i do? – setare shojaei Oct 26 '13 at 05:12
0

I think you should hide your folder when you are creating the folder by just putting a dot(.) before the name of folder. yes ,we can detect whether the folder exists or not. By using this:

File file=new File(your file path);
  if(file.exists){
   //your work
  }
Sunny
  • 11
  • 2