如何获取SD卡上添加音频的日期?

时间:2012-08-09 07:50:00

标签: android exception date audio fetch

我正在录制音频并将其与日期一起存储在SD卡中。这是我的代码 添加音频文件的日期和其他详细信息。

   audioFileName=input.getText().toString();           
current = System.currentTimeMillis();
    String artist = "" + getResources().getText(R.string.artistName);
   ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, audiofile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, audioFileName);               
   values.put(MediaStore.Audio.Media.DATE_ADDED, (int) current );
callMessage();
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.ARTIST, artist);
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri1 = contentResolver.insert(base, values);  

这是我的代码,我可以获取音频的所有细节。

   try {
        mAdapter = new SimpleCursorAdapter(
                this,
                // Use a template that displays a text view
                R.layout.media_select_row,
                // Give the cursor to the list adatper
                createCursor(""),
                // Map from database columns...
                new String[] {
                   String.valueOf(MediaStore.Audio.Media.DATE_ADDED ),
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media._ID},
                    // To widget ids in the row layout...
                    new int[] {
                    R.id.row_date,
                    R.id.row_album,
                    R.id.row_title,
                    R.id.row_icon,
                    R.id.row_options_button});

        setListAdapter(mAdapter);

        getListView().setItemsCanFocus(true);

        // Normal click - open the editor
        getListView().setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent,
                    View view,
                    int position,
                    long id) {
                startRecordingEditor();
            }                         
        });

    } catch (SecurityException e) {
        // No permission to retrieve audio?
        Log.e("RecordingApp", e.toString());

        // todo error 1
    } catch (IllegalArgumentException e) {
        // No permission to retrieve audio?
        Log.e("RecordingApp", e.toString());

        // todo error 2
    }

    mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view,
                Cursor cursor,
                int columnIndex) {
            if (view.getId() == R.id.row_options_button){
                // Get the arrow image view and set the onClickListener to open the context menu.
                ImageView iv = (ImageView)view;
                iv.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        openContextMenu(v);
                    }
                });
                return true;
            } else if (view.getId() == R.id.row_icon) {
                setSoundIconFromCursor((ImageView) view, cursor);
                return true;
            }

            return false;
        }
    });

它显示其他详细信息,但是当我想使用DATE_ADDED显示日期时,它显示了 跟随我无法理解的错误。

    08-09 13:15:52.519: E/AndroidRuntime(3265): FATAL EXCEPTION: main
    08-09 13:15:52.519: E/AndroidRuntime(3265): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blitze.recordingapp/com.blitze.recordingapp.RecorderListActivity}: java.lang.NullPointerException
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.view.View.performClick(View.java:3511)

08-09 13:15:52.519: E/AndroidRuntime(3265):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-09 13:15:52.519: E/AndroidRuntime(3265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at dalvik.system.NativeStart.main(Native Method)
08-09 13:15:52.519: E/AndroidRuntime(3265): Caused by: java.lang.NullPointerException
08-09 13:15:52.519: E/AndroidRuntime(3265):     at com.blitze.recordingapp.RecorderListActivity.onCreate(RecorderListActivity.java:149)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.app.Activity.performCreate(Activity.java:4465)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-09 13:15:52.519: E/AndroidRuntime(3265):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

我正在尝试尝试但无法从SD卡获取日期 请有人指点我正确的观点 在此先感谢。

2 个答案:

答案 0 :(得分:1)

我认为你应该改变一下你的方法。这是代码我是如何做到的。虽然它在你的活动中返回错误的date.first,但这样做。 `

myCursor = null;
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String[] STAR = { "*" };
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

try {
            myCursor = getActivity().getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                    null, null);
            count = myCursor.getCount();
            naming = new String[count];
            Log.d("TAG", "count" + count);
myCursor.moveToFirst();
            Log.d("TAG", "inside if after last called");
            do {

                music_column_index = myCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                name = myCursor.getString(music_column_index);

                String DATA = myCursor.getString(myCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                int duration = myCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
                int s = myCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                // myCursor.moveToPosition(i);
                Long tm = myCursor
                        .getLong(myCursor
                                .getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED));

                // String date = tm.toString();

                String dateString = DateFormat.format("MM/dd/yyyy",
                        new Date(tm)).toString();

                Log.d("TAG", "date:" + dateString);

                size = "" + s + "KB";
                // size = myCursor.getString(duration) + "KB"; //
                // String time = myCursor.getString(tm);
                Log.d("TAG", "size:" + size);
} while (myCursor.moveToNext());
            myCursor.close();

`

答案 1 :(得分:0)

崩溃:MediaStore.Audio.Media.DATE_ADDED不存在。
MediaStore.Audio.Media.DATE_MODIFIED时间为1970/01/01 00:00:00。
使用File.lastModified()代替!

SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{ MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if (view.getId() == R.id.textView_name) {
            String path = cursor.getString(columnIndex);
            ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
            return true;
        } else if (view.getId() == R.id.textView_size) {
            long filesize = cursor.getLong(columnIndex);
            ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
            return true;
        } else if (view.getId() == R.id.textView_time) {
            path = cursor.getString(columnIndex);
            File file = new File(path);
            Date date = new Date(file.lastModified());
            ((TextView) view).setText(SDF.format(date));
            return true;
        } else if (view.getId() == R.id.imageView_icon) {    
            ((ImageView) view).setImageResource(R.mipmap.audiofile);
            return true;
        }
        return false;
    }
});