android 藍芽檔案

2021-07-25 05:10:35 字數 3077 閱讀 5632

利用android 傳送檔案需要解決一下幾個問題。

1,傳送方選中檔案時如何獲取檔案位址?

以下這段**作用是返回你選擇檔案的uri,因此你要重寫

onactivityresult來獲取返回的uri。但是這裡有有兩種情況存在先看下圖。

當你選擇檔案管理的時候選擇其中的乙個檔案返回的uri為: file:///storage/emulated/0/tencent/qqfile_recv/%e9%a2%98%e7%9b%ae.txt

因此要獲取檔案的位址需要分兩種情況進行獲取。

情況一:你可以利用

一下**可以獲取在相簿中位址

string path=null;

contentresolver resolver =this.getcontentresolver();
這個是獲取content://media/external/images/media/530405後面的數字也就是_id

path = getfilepath(resolver, uri, uri.getlastpathsegment());

//
privatestring getfilepath(contentresolver resolver,uri uri,string _id) 

return_data;

}

情況二:這個uri你可以利用path=uri.getpath();這個方法獲取uri的路徑以下是我獲取選中檔案的路徑
/storage/emulated/0/tencent/qqfile_recv/題目.txt
2,接受方如何獲取傳送方的檔名及檔案型別?
這裡我是傳送方檔名和路徑前後放乙個標識然後將其寫入流中,接受方然後解析出檔案型別及檔名,
//傳送及讀取檔案,這裡傳送檔案要放在子執行緒中不然傳送大檔案時會導致主線程阻塞
classreceivefileextendsthread 

filepath= file.getpath();

}@override

public voidrun()catch(exception e)

while(true)else

flag++;

name= getfilename(content);

log.d("test","flag:"+flag+",name:"+name);

/*outputstream=null*/

;//解決

}outputstream= getoutputstream(filepath,name);

if(content.startswith("begin"))

outputstream.write(buff, 0, length);

}catch(exception e) }}

//乙個傳送只得到乙個

fileoutputstream

private synchronizedfileoutputstream getoutputstream(string path, string name)

outputstream=newfileoutputstream(receivefile);

log.d("test","receivefile:"+receivefile.getname());

}catch(exception e)

}else

returnoutputstream;}//

返回解析傳來的真實資料

private synchronized bytegetbytes(string content)

//根據傳送過來的資料解析出檔名

private synchronizedstring getfilename(string content)

//先將標識,檔案及檔名寫入流中,然後將檔案資料寫入流中

public voidsendfile(file file)

inputstream.close();

}catch(exception e) }}

Android 藍芽基礎

藍芽發展至今經歷了8個版本的更新。1.1 1.2 2.0 2.1 3.0 4.0 4.1 4.2。那麼在1.x 3.0之間的我們稱之為傳統藍芽 4.x開始的藍芽我們稱之為低功耗藍芽也就是藍芽ble,當然4.x版本的藍芽也是向下相容的。android手機必須系統版本4.3及以上才支援ble api。還...

Android藍芽開發

藍芽開發,也就那麼幾個操作,開啟 搜尋 配對 連線 通訊。其他的東西都是建立在這麼幾個之上。下面就來說說這幾個基本操作。使用前,首先加入藍芽的兩個基本許可權。然後他們基本圍繞這麼幾個類來用。這些類是什麼,慢慢來說。bluetoothadapter bluetoothadapter bluetooth...

Android藍芽程式設計

問題一 在android藍芽程式設計中,有時可以連線,有時不可以連線。不可以連線的原因是 在執行socket.connect 時,總丟擲ioexception unable to start service discovery.問題二 可是在執行下面 bluetoothrev類時,怎樣才能執行到符合...