package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
时间: 2024-04-27 18:24:06 浏览: 163
这是一段Java代码,看起来是一个获取本地音乐列表的帮助类。它使用了Android提供的MediaStore来查询本地的音频文件,然后将查询到的结果封装成一个Song对象,并保存在一个List中。可以通过调用`getLocalMusic()`方法来获取本地音乐列表。其中,每一个Song对象代表一首音乐,包含了音乐的名称、歌手、路径、时长等信息。`changeTime()`方法用于将时间格式从毫秒转换为分秒形式。
相关问题
package com.example.login; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.app.AlertDialog; import android.content.DialogInterface; import android.widget.Spinner; import android.widget.Toast;
这是一个 Android 应用程序中的 Java 代码片段,用于实现一个登录功能。它包含了一些 Android 库的导入,如 `AppCompatActivity`,`ListView` 等等。这段代码还实现了与数据库的交互,通过 `SQLiteDatabase`、`Cursor`、`ContentValues` 等类进行数据的增删改查操作。此外,还包含了一些 UI 组件的定义,如 `EditText`、`Button`、`Spinner` 等等。最后,还有一些弹出框的实现,如 `AlertDialog`、`Toast` 等等。
package com.example.photodiary; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class Diary extends AppCompatActivity { public static final int REQUEST_CODE_CAMERA = 1001; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_diary); /* //设置 RecyclerView 的布局管理器和适配器 RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new MyAdapter()); */ //点击按钮启动系统相机 Button button = findViewById(R.id.button_2); Intent intent = new Intent(this,CameraX.class); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(intent); } }); } }
这段代码是一个名为 "Diary" 的活动类,继承自 "AppCompatActivity" 类。在这个活动中,有一个常量 "REQUEST_CODE_CAMERA",它的值为 1001,用于在启动相机时标识请求。在 "onCreate" 方法中,调用了父类的 "onCreate" 方法,并设置了活动内容视图为 "activity_diary" 布局。另外,还有一个按钮 "button_2",当点击这个按钮时,启动名为 "CameraX" 的活动。
这段代码的注释中还有一些被注释掉的代码,它们是设置 RecyclerView 布局管理器和适配器的代码。可能是因为这部分代码暂时不需要,所以被注释掉了。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)