【问题标题】:copy folder containing 10 mp3 files from Assets to SD-card将包含 10 个 mp3 文件的文件夹从 Assets 复制到 SD 卡
【发布时间】:2013-02-02 23:08:54
【问题描述】:

我正在为Electrum Drum Machine/Sampler 开发一个安卓免费架子鼓应用程序。

我需要将包含 10-20 mp3 文件的文件夹从 Assets 复制到 SD-card。 我需要包含文件的文件夹到这里“sdcard/electrum” 还有我在代码中到底在哪里插入路径“sdcard/electrum”!

我是编码新手,但学起来很快

这是我的代码...

package com.example.newsampletest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

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

        copyAssets();

    }

    private void copyAssets() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("files");
        } catch (IOException e) {

        }
        for(String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);
                out = new FileOutputStream("files/"+filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }
}

【问题讨论】:

  • 请提供堆栈跟踪。

标签: android assets android-sdcard


【解决方案1】:

我的设备出现错误“安装失败。请检查文件并确保手机有足够的空间进行安装”

这表明您的 APK 文件太大,并且您的设备没有足够的内部存储空间。

你可以:

  • 卸载现有应用以尝试腾出空间,或
  • 尝试使用较短的音频剪辑,或
  • 降低音频剪辑质量(例如,降低比特率),或
  • 使用更少的音频剪辑,或
  • 不要将部分或全部剪辑与您的应用一起打包,而是在首次运行时将一些剪辑下载到外部存储中,或者
  • 使用app install location,虽然我不完全确定这对您的情况有帮助

【讨论】:

  • 我只是测试我的代码。所以资产中只有一个文件。 apk 只有 249 KB
  • @antony:那么问题更多在于您的设备空间不足。
  • "安装失败。解决了..谢谢。仍然需要让资产去这里"sdcard/electrum"
猜你喜欢
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
相关资源
最近更新 更多