【问题标题】:Android decode and save audio stream returned from intentAndroid解码并保存从意图返回的音频流
【发布时间】: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


    【解决方案1】:

    我通过从意图中获取文件路径并按原样复制文件而不是对其进行解码来使其工作。我查看的所有文件都没有明确的格式(它们不是 .wav 或 .mp3,没有列出文件类型)

    这就是我的代码在声音部分的 onActivityResult 中的样子

    InputStream stream = getContentResolver().openInputStream(data.getData());
    String FilePath = data.getData().getPath();
    File original = new File(FilePath);
    
    File deletefile = new File(savepath);
    String newpath = deletefile.getParent() + "/" +  original.getName();
    deletefile.delete();
    
    File newfile = new File(newpath);
    FileOutputStream out = new FileOutputStream(newfile);
    
    int read = 0;
    byte[] bytes = new byte[1024];
    
    while ((read = stream.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }
    
    out.close();
    stream.close();
    

    【讨论】:

      【解决方案2】:

      Kotlin 1.2

          fun saveAudio(myAudio:Intent?):String {//TURN ON PERSMISSIONS ON THE EMULATOR IN SETTINGS
          //var bytes = ByteArrayOutputStream()
          val audioURI = myAudio!!.data
      
      
          try {
              val audioDirectory = File(
                      (Environment.getExternalStorageDirectory()).toString() + AUDIO_DIRECTORY)
      
              val out = File(audioDirectory, ("fullAudio" + ".mp3"))
      
              if (!audioDirectory.exists()){
                  audioDirectory.mkdirs()
              }
      
              out.createNewFile()//cressh
      
              val file = File(audioURI.path)
      
      
              val fo = FileOutputStream(out)
              //val input = FileInputStream(f)
      
              var stream = contentResolver.openInputStream(audioURI)
              var read = 0
      
      
      
              fo.write(stream.readBytes(1024))
      
      
              MediaScannerConnection.scanFile(this,
                      arrayOf(file.getPath()),
                      arrayOf("audio/mpeg"),null)
      
              fo.close()
              return file.absolutePath
      
          }catch (e1: IOException){
              e1.printStackTrace()
          }
          return ""
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-27
        • 2011-05-23
        • 2013-06-24
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多