在我的 android 应用程序中,我想将文件raw夹中的音频文件设置为铃声。为此,我写了下面的代码,但它不起作用。
请帮我解决这个问题。谢谢你。
代码 :
String name =best_song_ever.mp3;
File newSoundFile = new File("/sdcard/media/ringtone",
"myringtone.mp3");
Uri mUri = Uri.parse("android.resource://"
+ context.getPackageName() + "/raw/" + name);
ContentResolver mCr = context.getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile = mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile = null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
} …Run Code Online (Sandbox Code Playgroud) android mediastore android-contentresolver android-contentprovider android-mediaplayer
所以我有这个代码用于使用 mediastore 获取图像:
int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA );
mMediaStoreCursor.moveToPosition(position);
String dataString = mMediaStoreCursor.getString(dataIndex);
Uri mediaUri = Uri.parse("file://" + dataString);
return mediaUri;
Run Code Online (Sandbox Code Playgroud)
这很好地获取图片文件夹中的所有图像,我想更改它以获取特定文件夹中的所有图像,该文件夹将作为字符串传递。例如{ android/picture/specificfolder/}
private List<PDFFileDetails> getPdfFilesFromDevice(Context context) {
List<PDFFileDetails> listOfDirectories = new ArrayList<>();
ContentResolver cr = context.getContentResolver();
Uri uriExternal = MediaStore.Files.getContentUri("external");
Uri uriInternal = MediaStore.Files.getContentUri("internal");
String[] projection = null;
String sortOrder = null; // unordered
// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
Run Code Online (Sandbox Code Playgroud)
在这里,我从内部存储而不是外部 sdcard 中获取所有 pdf 首先我尝试不使用 sdcard,然后我插入了 sdcard 但没有结果来自 sdcard
Cursor allPdfFilesCursor = cr.query(uriExternal, projection, selectionMimeType, selectionArgsPdf, sortOrder);
if (allPdfFilesCursor != null && allPdfFilesCursor.getCount() != 0) {
allPdfFilesCursor.moveToFirst();
do {
int …Run Code Online (Sandbox Code Playgroud) 我正在使用鲍雷提供的代码,名称为private fun saveImage在如何将位图保存到 android gallery来保存位图
为了解决 Kotlin 中已弃用的问题,我删除了这些行:
//values.put(MediaStore.Images.Media.DATA, file.absolutePath)
//context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
Run Code Online (Sandbox Code Playgroud)
并添加了这个:
val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, folderName)
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Run Code Online (Sandbox Code Playgroud)
然后,我将该函数(在 DidtodayActivity 中)称为:
saveImage(bitmapImg, this.applicationContext)
Run Code Online (Sandbox Code Playgroud)
图像被下载到Phone>Android>Data>com.example.MyApp1>files>MyFiles。我可以在我的设备中看到下载的图像。
但是,由于这一行的运行时错误,活动文件突然关闭(并且直接打开activity_main .xml):
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Run Code Online (Sandbox Code Playgroud)
原因:
java.lang.ClassNotFoundException: Didn't find class "android.provider.MediaStore$Downloads" on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]
Run Code Online (Sandbox Code Playgroud)
详细信息:
**Didn't find class "android.provider.MediaStore$Downloads"** on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]
Run Code Online (Sandbox Code Playgroud)
解析失败:Landroid/provider/MediaStore$Downloads;
模块级别的build.gradle是:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android { …Run Code Online (Sandbox Code Playgroud) 我开始一个ACTION_GET_CONTENT意图来选择 PDF:
override fun routeToFilePicker() {
val intent = Intent()
intent.type = MediaType.PDF.toString()
intent.action = Intent.ACTION_GET_CONTENT
activity.startActivityForResult(
Intent.createChooser(intent, "Select PDF"),
REQUEST_CODE_PDF_PICKER
)
}
Run Code Online (Sandbox Code Playgroud)
然后onActivityResult我尝试从 Uri ( ) 创建 PDF content//:path:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_PDF_PICKER ) {
data?.data?.let { pdfUri: Uri ->
val pdfFile: File = pdfUri.toFile() <-- chrash
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
pdfUri.toFile()导致致命异常:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1003, result=-1, data=Intent …Run Code Online (Sandbox Code Playgroud) 有没有办法通过使用MediaPLayer播放从MediaStore获得的音频,或者我是否朝着完全错误的方向前进?到目前为止我看过MediaStore.Audio,但没有什么能真正帮助我.我只需要知道我是否走在正确的轨道上
我正在创建一个Android应用程序,利用相机拍照,然后根据用户输入将它们发送到服务器.我目前在相机意图方面遇到了一些问题.我的主要问题是:
获取与拍摄位置相比似乎旋转的照片.
当我尝试修复此旋转时,我得到一个OutOfMemoryError
所以主要是我需要确保图片的方向不会改变,并且我没有得到OutOfMemoryError.
这是使用相机Intent拍摄照片的功能.
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
}
Run Code Online (Sandbox Code Playgroud)
此功能用于旋转图像并将其保存在该旋转处.
private void getRotate() {
String imagePath ="";
int rotate = 0;
File f = null;
try {
f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
imagePath = f.getAbsolutePath();
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = …Run Code Online (Sandbox Code Playgroud) android bitmap out-of-memory mediastore android-camera-intent
我想写一个从整个SD卡中获取所有mp3文件的类.但实际上它只能将音频文件直接放在SD卡上.因此它不会搜索子文件夹.我想使用Mediastore但我不会把它带到一个arraylist.也许你可以帮助我,非常感谢并且抱歉做了一个菜鸟;)vinzenz
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import de.me.musicplayer.SongsManager.FileExtensionFilter;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
public class SongsManager {
final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) …Run Code Online (Sandbox Code Playgroud) 我想使用MediaStore在手机上获得所有艺术家的名字;已经有了整个列表,但由于有多个链接歌曲/艺术家,所以所有条目都有重复。有什么方法可以使用查询来获取每个查询中的一个,而没有任何重复项吗?
有我的查询:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DURATION
};
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
Run Code Online (Sandbox Code Playgroud)
提前致谢 !
我正在尝试从Fragment中的用户的音乐文件夹中获取音乐文件,但是当我这样做并在onActivityResult中接收它(并记录它)后,当我想用mediaplayer播放它时,我得到一个Nullpointer异常.选择文件的代码:
Button pickFile = (Button) v.findViewById(R.id.btnpick);
pickFile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
});
Run Code Online (Sandbox Code Playgroud)
OnActivityResult(不要忘记我在片段中)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getActivity();
if ((resultCode == getActivity().RESULT_OK) && (data != null)) {
Log.d("result", "okreceived");
Uri soundUri = Uri.parse(data.getData().toString());
Log.i("result", "Intent data " + data.getData().toString());
mplayergo(soundUri);
}
if (resultCode == getActivity().RESULT_CANCELED)
{
//null
}
}
public void mplayergo(Uri soundUri){
try {
mplayer.setDataSource(getActivity(), soundUri); …Run Code Online (Sandbox Code Playgroud) android nullpointerexception mediastore android-intent android-fragments