gpt4 book ai didi

java - 有没有一种有效的方法来检查子目录并通过Android Assets 文件夹中的相对路径加载 Assets ?

转载 作者:行者123 更新时间:2023-12-01 15:27:43 39 4
gpt4 key购买 nike

这是我的代码,尽管编码非常粗糙:

public void loadStack(AssetManager manager, String path) {
String[] list;
String thePath;
try {
list = manager.list(path);
if (list != null) {
for (int i = 0; i < list.length; i++) {
try {
if (list.length != 0) {
if (path.toString() == "") loadStack(manager, list[i]);
else {
thePath = path + "/" + list[i];
loadStack(manager, thePath);
}
}
String[] test = manager.list(list[i]);
if (test.length != 0) {
for (int j = 0; j < test.length; j++) {
byte[] assetBytes = readFromStream(list[i] + "/" + test[j]);
assetStack.push(assetBytes);
totalByteSize += assetBytes.length;
Log.d("Loading", "Stack loads assetBytes of length: " + Integer.toString(assetBytes.length));
// totalStackElementSize = assetStack.size();
}
}
// loadStack(manager, path + "/" + list[i]);
}
catch (IOException e) {
continue;
}
}
}
}
catch (IOException e1) {
return;
}
}

我仍在尝试改进代码,我所缺少的只是区分哪些相对路径是子目录,哪些相对路径是需要加载的文件的实际路径。

这样,我就可以依靠使用递归方法来遍历资源文件夹的每个子目录,并仅加载我需要的子目录。

有人能指出我正确的方向吗?或者我在 AssetManager 文档中遗漏了一些东西?提前致谢。到目前为止我已经尽力了。

更新:

将我的代码改进为:

public void loadStack(AssetManager manager, String path, int level) {
try {
String[] list = manager.list(path);
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (level >= 1) loadStack(manager, path + "/" + list[i], level + 1);
else if (level == 0) loadStack(manager, list[i], level + 1);
else {
byte[] byteBuffer = readFromStream(path);
assetStack.push(byteBuffer);
totalByteSize += byteBuffer.length;
}
}
}
}
catch (IOException e) {
Log.e("Loading", "Occurs in AssetLoad.loadStack(AssetManager, String, int), file can't be loaded: " + path);
throw new RuntimeException("Couldn't load the files correctly.");
}
}

我仍在努力改进我的代码。唯一的问题是它倾向于读取我尚未添加到 Assets 文件夹中的文件夹,或者我以前从未创建过的文件夹。这些会导致逻辑错误,我会尽可能避免这种情况。

最佳答案

这是对一个模糊问题的具体答案,但其工作原理是相同的。

package nttu.edu.activities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;

import nttu.edu.R;
import nttu.edu.graphics.Art;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class NewLoadingActivity extends Activity {
public ProgressBar bar;
private AssetManager assetManager;
public Handler handler;
public ProgressTask task;

private final String[] list = {
// Art.sprites
"art/sprites.png" };

private class ProgressTask extends AsyncTask<Void, Void, Void> {
public int totalByteSize;
public int currentByteSize;
public Queue<Bitmap> bitmapQueue;
public Queue<byte[]> byteQueue;

public ProgressTask() {
totalByteSize = 0;
currentByteSize = 0;
bitmapQueue = new LinkedList<Bitmap>();
byteQueue = new LinkedList<byte[]>();
}

public void onPostExecute(Void params) {
Art.sprites = bitmapQueue.remove();
finish();
}

public void onPreExecute() {
try {
for (int i = 0; i < list.length; i++) {
byte[] bytes = readFromStream(list[i]);
totalByteSize += bytes.length;
byteQueue.add(bytes);
}
bar.setMax(totalByteSize);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}

public void onProgressUpdate(Void... params) {
bar.setProgress(currentByteSize);
}

@Override
protected Void doInBackground(Void... params) {
while (currentByteSize < totalByteSize) {
try {
Thread.sleep(1000);
if (byteQueue.size() > 0) {
byte[] bytes = byteQueue.remove();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
bitmapQueue.add(bitmap);
currentByteSize += bytes.length;
this.publishProgress();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}

private byte[] readFromStream(String path) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
InputStream input = assetManager.open(path);
while (input.available() > 0 && (length = input.read(buffer)) != -1)
output.write(buffer, 0, length);
return output.toByteArray();
}

}

public void onCreate(Bundle b) {
super.onCreate(b);
this.setContentView(R.layout.progressbar);
assetManager = this.getAssets();
handler = new Handler();
task = new ProgressTask();
bar = (ProgressBar) this.findViewById(R.id.loadingBar);
if (bar == null) throw new RuntimeException("Failed to load the progress bar.");
task.execute();
}

public void finish() {
Intent intent = new Intent(this, MenuActivity.class);
intent.putExtra("Success Flag", Art.sprites != null);
this.setResult(RESULT_OK, intent);
super.finish();
}
}

关于java - 有没有一种有效的方法来检查子目录并通过Android Assets 文件夹中的相对路径加载 Assets ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968218/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com