使用解码字节时的outofmemoryexception

时间:2015-10-16 08:50:01

标签: java android bitmap out-of-memory

我正在创建媒体播放器,而我正在使用MediaMetadataRetriever来获取我的音频文件所拥有的所有照片。它工作正常,当我有少量的音频文件,但当有很多文件,例如100-150,我有 的 OutOfMemoryException异常

以下是我获取图片的代码:

public class AudioDetails {
    private final static String TAG="NO PICTURE";
    private String albums[];
    private String durations[];
    private String singers[];
    private ArrayList<String> audioTitles;
    private ArrayList<String> audioPaths;
    private ArrayList<Bitmap> audioPic;
    private boolean tested=false;
    private MediaMetadataRetriever mediaDetails= new MediaMetadataRetriever();

    private AudioDetails(ArrayList<String> audioPaths){
        this.audioPaths=new ArrayList<>(audioPaths);
        albums=new String[audioPaths.size()];
        durations=new String[audioPaths.size()];
        singers=new String[audioPaths.size()];
        audioPic=new ArrayList<>();
        audioTitles=new ArrayList<>();
        processDuration();
    }

    private String getName(String filePath){
        StringBuilder audioName=new StringBuilder("");
        for(int i=filePath.length()-1;i>=0
            &&filePath.charAt(i)!='/';i--)
            audioName.append(filePath.charAt(i));
        audioName.reverse();
        try{
            if(filePath.contains(".mp3")
                ||filePath.contains(".ogg")){
               audioName.delete(filePath.length()-4,filePath.length()-1);
            }
        }catch (Throwable e){
            e.printStackTrace();
        }
        return audioName.toString();
    }

    public void processTitle(){
        String title;
        for(int i=0;i<audioPaths.size();i++){
            mediaDetails.setDataSource(audioPaths.get(i));
            title=mediaDetails.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR);
            audioTitles.add(title!=null&&!title.equals(" ")?title:"<unknown>");
        }
    }

    private void processDuration(){
        int min,sec;
        for(int i=0;i<audioPaths.size();i++){
            mediaDetails.setDataSource(audioPaths.get(i));
            sec=Integer.parseInt(mediaDetails.extractMetadata
                    (MediaMetadataRetriever.METADATA_KEY_DURATION)) / 1000;
            min=sec/60;
            sec=sec%60;
            durations[i]=Integer.toString(min)+":"+(sec>=10?
                    Integer.toString(sec):"0"+Integer.toString(sec));
        }
    }

    public void processPictures(){
        MediaMetadataRetriever mediaDetails= new MediaMetadataRetriever();
        for(int i=0;i<audioPaths.size();i++){
            mediaDetails.setDataSource(audioPaths.get(i));
            byte[] pic=mediaDetails.getEmbeddedPicture();
            if(pic!=null)
                audioPic.add(BitmapFactory.decodeByteArray(pic,0,pic.length));
            else{
                Log.e(TAG,"No picture");
                audioPic.add(null);
            }
            tested=true;
        }
    }

    public static AudioDetails create(ArrayList<String> audioPath){
        return new AudioDetails(audioPath);
    }

    public String[] getAudioDurations(){
        return durations;
    }

    public String[] getAudioSingers(){
        return singers;
    }

    public String[] getAlbums(){
        return albums;
    }

    public ArrayList<Bitmap> getAudioFrames(){
        return audioPic;
    }

    public ArrayList<String> getAudioTitles(){
        return audioTitles;
    }
}

也许你知道如何解决这个问题? 请帮帮我。

0 个答案:

没有答案