虽然使用Android的Underdark SDK,但我想显示文件传输进度。
以下是暗节点类:
public class Node implements TransportListener
{
private boolean running;
private MainActivity activity;
private long nodeId;
private Transport transport;
private ArrayList<Link> links = new ArrayList<>();
private int framesCount = 0;
public Node(MainActivity activity)
{
this.activity = activity;
do
{ nodeId = new Random().nextLong();
} while (nodeId == 0);
if(nodeId < 0)
nodeId = -nodeId;
configureLogging();
EnumSet<TransportKind> kinds = EnumSet.of(TransportKind.BLUETOOTH, TransportKind.WIFI);
//kinds = EnumSet.of(TransportKind.WIFI);
//kinds = EnumSet.of(TransportKind.BLUETOOTH);
this.transport = Underdark.configureTransport(
234235,
nodeId,
this,
null,
activity.getApplicationContext(),
kinds
);
}
private void configureLogging()
{
NSLoggerAdapter adapter = (NSLoggerAdapter)
StaticLoggerBinder.getSingleton().getLoggerFactory().getLogger(Node.class.getName());
adapter.logger = new NSLogger(activity.getApplicationContext());
adapter.logger.connect("192.168.5.203", 50000);
Underdark.configureLogging(true);
}
public void start()
{
if(running)
return;
running = true;
transport.start();
}
public void stop()
{
if(!running)
return;
running = false;
transport.stop();
}
public ArrayList<Link> getLinks()
{
return links;
}
public int getFramesCount()
{
return framesCount;
}
public void broadcastFrame(byte[] frameData)
{
activity.refreshFrames();
if(links.isEmpty())
return;
++framesCount;
activity.refreshFrames(frameData);
for(Link link : links) {
link.sendFrame(frameData);
}
}
//region TransportListener
@Override
public void transportNeedsActivity(Transport transport, ActivityCallback callback)
{
callback.accept(activity);
}
@Override
public void transportLinkConnected(Transport transport, Link link)
{
links.add(link);
activity.refreshPeers();
}
@Override
public void transportLinkDisconnected(Transport transport, Link link)
{
links.remove(link);
activity.refreshPeers();
if(links.isEmpty())
{
framesCount = 0;
activity.refreshFrames();
}
}
@Override
public void transportLinkDidReceiveFrame(Transport transport, Link link, byte[] frameData)
{
activity.refreshFrames(frameData);
}
}
以及加载视频和初始化传输的Main活动类如下:
public class MainActivity extends AppCompatActivity {
private List<String> selectedVideos = new ArrayList<>();
private Node node;
boolean isClientApp = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
node = new Node(this);
}
@Override
protected void onStart() {
super.onStart();
node.start();
}
@Override
protected void onStop() {
super.onStop();
if (node != null)
node.stop();
}
public void sendFrames(byte[] bytes) {
node.broadcastFrame(bytes);
}
public void refreshPeers() {
}
public void refreshFrames(final byte[] d) //receive callback on bytes transfer completion
{
convertBytesToFile(d);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
selectedVideos = getSelectedVideos(requestCode, data);
convertFile(path).execute();
}
}
private class convertFile extends AsyncTask<Integer, Integer, byte[]> {
int x = 0;
double progress;
int full = 0;
public convertFile(String path) {
try {
fis = new FileInputStream(path);
full = ((fis.available() / b.length) + 1);
} catch (IOException e) {
Log.d("excaption", "zvaita");
}
}
FileInputStream fis;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
@Override
protected void onPostExecute(byte[] result) {
publishProgress(100);
Log.d("postexec", "receiving file");
sendFrames(result);
}
@Override
protected byte[] doInBackground(Integer... integers) {
try {
for (int readNum; (readNum = fis.read(b)) != -1; ) {
bos.write(b, 0, readNum);
sendFrames(b);
++x;
progress = ((double) x / full) * 100;
publishProgress((int) progress);
}
byte[] bytes = bos.toByteArray();
return bytes;
} catch (IOException e) {
return null;
}
}
@Override
protected void onPreExecute() {
publishProgress(0);
if (!isClientApp)
Log.d("preexec", "receiving file");
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.d("progress", String.valueOf(progress[0]));
}
}
private void convertBytesToFile(byte[] bytearray) {
try {
File outputFile = File.createTempFile("keepsync", ".mp4", getCacheDir());
outputFile.deleteOnExit();
FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
fileoutputstream.write(bytearray);
fileoutputstream.close();
// Toast.makeText(getApplicationContext(),outputFile.toString().toString(),Toast.LENGTH_LONG).show();
try {
if (new File(outputFile.getAbsolutePath()).exists()) {
//doing something with transferred file
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我试图显示在2台设备之间传输文件的进度,但是一直失败。我已经考虑过在
中发送字节node.broadcastFrame(字节)
成块,但完全无法把握何时检测字节何时完成。多数情况下,我会遇到内存不足的异常,此时,我愿意寻求帮助。
谢谢