【发布时间】:2013-12-01 20:16:32
【问题描述】:
我希望用户能够在我的应用中导入自定义声音以更改默认声音。我已经有这个功能适用于位图,但我也想扩展到声音。我缺少的关键步骤是音频解码。我不知道声音会采用什么格式,所以我需要先对音频进行解码,然后再将其保存到内部存储中。对于位图,这是由 bitmapfactory 和 bitmap 对象完成的,但我找不到类似的音频服务。这是我到目前为止的代码。位图部分有效,但音频部分不完整。:
private void retrievepicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);//retrieve picture has a code of 1
}
private void retrievesound() {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 2);//retrieve sound has a code of 2
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
usingintents = false;
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
try {
InputStream stream = getContentResolver().openInputStream(
data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
stream.close();
File deletefile = new File(savepath);
System.out.println(String.format("Replacing file %s",deletefile.getPath()));
deletefile.delete();
saveImageToInternalStorage(bitmap,savepath);
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (requestCode == 2 && resultCode == Activity.RESULT_OK)
{
//The code for saving audio would go here
InputStream stream = getContentResolver().openInputStream(
data.getData());
}
super.onActivityResult(requestCode, resultCode, data);
}
public boolean saveImageToInternalStorage(Bitmap image, String filepath) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = new FileOutputStream(filepath);
// Writing the bitmap to the output stream
if(image.getWidth() >480 || image.getHeight() > 480)
image = Bitmap.createScaledBitmap(image, 480, 480, false);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
【问题讨论】:
标签: android audio android-intent bitmap decoding