'The date of audio file is same for all audio file

When I run this code I get all the list of image give below audio files but, the date of all the audio files are same like 20 Jan 1970 . below the code of AudioActivity.java. I don't know how I can do it if is possible .

public class AudioActivity extends AppCompatActivity {

Adapter adapter;
RecyclerView recyclerView;

private SlideAdapter slideAdapter;

public static final int PERMIT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio);

    Toolbar toolbar = findViewById(R.id.aa_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("Call Tank");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    recyclerView = findViewById(R.id.recyclerview);

    fetchSongs();
}


private void fetchSongs() {

    //define list to carry songs
    List<ModelClass> songs = new ArrayList<>();
    Uri songLibraryUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        songLibraryUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        songLibraryUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    }

    //projection
    String[] projection = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DATE_MODIFIED,
            MediaStore.Audio.Media.DATA
    };

    //sort order
    String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC";

    //Querying
    try (Cursor cursor = getContentResolver().query(songLibraryUri, projection, null, null, sortOrder)) {

        // cache the cursor indices
        int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
        int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
        int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED);
        int pathColimn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);


        //getting the values
        while (cursor.moveToNext()) {

            // get values of colums for a give audio files
            long id = cursor.getLong(idColumn);
            String name = cursor.getString(nameColumn);
            long date = cursor.getLong(dateColumn);
            String path = cursor.getString(pathColimn);

            //song uri
            Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

            //remove .mp3 extension on song's name
            name = name.substring(0, name.lastIndexOf("."));

            // song item
            ModelClass song = new ModelClass(path, name, uri, date);
            // add song to songs list
            songs.add(song);

        }

        Intent i = new Intent(AudioActivity.this,Onboarding.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("songs", (Serializable) songs);
        i.putExtras(bundle);
        startActivity(i);

        //Intent intent = new Intent(AudioActivity.this,Onboarding.class);
      //  intent.putExtra("songs", (Serializable) songs);
        //show songs on rv
        showSongs(songs);
        Toast.makeText(this, "Number of Songs:" + songs.size(), Toast.LENGTH_SHORT).show();
    }
}

private void showSongs(List<ModelClass> songs) {
    // songs.clear();
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(RecyclerView.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new Adapter(songs);
    recyclerView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    item.getItemId();
    return super.onOptionsItemSelected(item);
}

}

Below the code of Adapter.java

     public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

List<ModelClass> songs;
public Adapter(List<ModelClass> songs) {
    this.songs = songs;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

  //  View view = LayoutInflater.from(parent).inflate(R.layout.item_layout,parent,false);
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_layout,parent,false);

    return new ViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        ModelClass modelClass = songs.get(position);

        String file = modelClass.getFilename();
        holder.fileName.setText(file);

        Long dateTime = modelClass.getDate();
        String currentDate = DateFormat.getDateInstance().format(dateTime);
        holder.date.setText(currentDate);

        MediaPlayer mediaPlayer = new MediaPlayer();
        String audioPath = modelClass.getPath();
        try {
            mediaPlayer.setDataSource(audioPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        holder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                try {
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mP) {
                            mP.start();
                            holder.play.setVisibility(View.INVISIBLE);
                            holder.pause.setVisibility(View.VISIBLE);
                        }
                    });

                    mediaPlayer.prepare();


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        holder.pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
                holder.play.setVisibility(View.VISIBLE);
                holder.pause.setVisibility(View.INVISIBLE);
            }
        });

}

@Override
public int getItemCount() {
    return songs.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView play , pause;
    TextView fileName, date;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        play = itemView.findViewById(R.id.play);
        pause= itemView.findViewById(R.id.pause);
        fileName = itemView.findViewById(R.id.fileName);
        pause.setVisibility(View.INVISIBLE);
        date = itemView.findViewById(R.id.date);
        } 
      }
   } // the code end

Date of all the audio files are same in the list

This is ModalClass.java

public class ModelClass  {

String path,filename;
Uri uri;
Long date;

public ModelClass(String path, String filename,   Uri uri, Long date) {
    this.path = path;
    this.filename = filename;
    this.date = date;
    this.uri = uri;
}

public ModelClass() {
}

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public String getFilename() {
    return filename;
}

public void setFilename(String filename) {
    this.filename = filename;
}

public Uri getUri() {
    return uri;
}

public void setUri(Uri uri) {
    this.uri = uri;
}

public Long getDate() {
    return date;
}

public void setDate(Long date) {
    this.date = date;
}

}



Solution 1:[1]

Long dateTime = modelClass.getDate()

You did not post the code for getDate() while there starts your problem.

It problably returns 0 for all files.

And 0 equals 20 Jan 1970.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blackapps