Возможность использования ресурсов внутри приложения

This commit is contained in:
Victor 2015-06-16 00:45:29 +03:00
parent 9964f2b346
commit a7ba5d6398
2 changed files with 28 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
@ -23,8 +24,12 @@ import android.os.Environment;
*/
public final class IOUtil {
private static String SDCARD = Environment.getExternalStorageDirectory().getPath();
private static String ES = SDCARD + "/everlastingsummer/";
public static boolean useArchive = false;
public static String ASSETS = getSdCardPath() + "everlastingsummer/";
public static String getSdCardPath() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
public static Bitmap readBitmap(String file) throws IOException {
final InputStream is = open(file);
@ -69,15 +74,20 @@ public final class IOUtil {
}
}
public static AssetFileDescriptor getAFD(String file) throws IOException {
return ViewActivity.getInstance().getAssets().openFd(ASSETS + file);
}
private static FileInputStream streamForFD;
public static FileDescriptor getFD(String file) throws IOException {
if (streamForFD != null) streamForFD.close();
streamForFD = new FileInputStream(ES + file);
streamForFD = new FileInputStream(ASSETS + file);
return streamForFD.getFD();
}
public static InputStream open(String file) throws IOException {
return new FileInputStream(ES + file);
if (useArchive) return ViewActivity.getInstance().getAssets().open(ASSETS + file);
return new FileInputStream(ASSETS + file);
}
public static String readContents(InputStream is) throws IOException {

View File

@ -1,5 +1,6 @@
package com.annimon.everlastingsummer;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
@ -14,6 +15,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
@ -528,7 +530,12 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
stopMusic(fade);
musicPlayer = new MediaPlayer();
musicPlayer.setOnCompletionListener(musicCompleteListener);
if (IOUtil.useArchive) {
final AssetFileDescriptor afd = IOUtil.getAFD(PathResolver.music(name));
musicPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} else {
musicPlayer.setDataSource( IOUtil.getFD(PathResolver.music(name)) );
}
musicPlayer.prepare();
musicPlayer.setVolume(1f, 1f);
musicPlayer.setLooping(true);
@ -552,7 +559,12 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
stopSound(fade);
soundPlayer = new MediaPlayer();
soundPlayer.setOnCompletionListener(soundCompleteListener);
if (IOUtil.useArchive) {
final AssetFileDescriptor afd = IOUtil.getAFD(PathResolver.sound(name));
soundPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} else {
soundPlayer.setDataSource( IOUtil.getFD(PathResolver.sound(name)) );
}
soundPlayer.prepare();
soundPlayer.setVolume(1f, 1f);
soundPlayer.setLooping(loop);