導航:首頁 > 操作系統 > android訪問本地文件的文件

android訪問本地文件的文件

發布時間:2022-08-29 14:42:43

android 中如果我想訪問本地文件e:\test.txt,要如何賦值String test="http://10.0.2.2:8080" 後面加什麼

你是想讀取手機sdcard里的文件還是遠端伺服器上的文件。如果是sdcard上的文件,知道文件的路徑就可以調用函數進行讀取了,如果是遠端伺服器上的,你得寫一個項目放到伺服器下讓手機通過網路來獲取。

② Android編程 打開本地文件 文件選擇器

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/b01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

代碼

java">import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Lesson_01_Pic extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button)findViewById(R.id.b01);
button.setText("選擇圖片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 開啟Pictures畫面Type設定為image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片後返回本畫面 */
startActivityForResult(intent, 1);
}

});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 將Bitmap設定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

③ android 使用URL獲取本地的文件

public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}

④ Android: 如何訪問項目文件夾下的文件

Android中讀取assets文件夾裡面的文件,可使用SDK的API,需要用AssetManager以位元組流的形式讀取文件。
assets的讀取方式:
1. 先在Activity裡面調用getAssets() 來獲取AssetManager引用。
2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。
3. 然後就是用已經open file 的inputStream讀取文件,讀取完成後記得inputStream.close() 。
4.調用AssetManager.close() 關閉AssetManager。
需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進行寫的操作。

⑤ 如何用android手機訪問電腦上的文件

那必須要處於同一區域網內。
譬如你有無線路由器,電腦和手機用的都是這個路由器的網路。
然後手機上下載ES文件瀏覽器,同時把電腦上想要訪問的文件夾設置為共享狀態,手機可以通過ES文件瀏覽器來訪問電腦上的文件了。
另外在區域網內,可以通過teamviewer遠程式控制制電腦,當然,手機和電腦要同時安裝teamviewer的客戶端,免費的。

⑥ Android如何訪問本地的資料庫

你可以選擇使用文件管理器。如果你是4.0以上的,應該可以不用軟體,自帶。不過,我可以給你推薦一個軟體,它又是管家,也可以看文件,LBE安全大師,真的很好用。

閱讀全文

與android訪問本地文件的文件相關的資料

熱點內容
西安哪個app能買到東西 瀏覽:457
eps命令鍵 瀏覽:37
塑料文件夾的尺寸 瀏覽:210
君威壓縮機電磁閥 瀏覽:945
安卓6手機為什麼不能用app 瀏覽:860
什麼java編譯器支持中文 瀏覽:563
香港伺服器如何做代理 瀏覽:201
pdf寫入 瀏覽:986
高爾夫電台怎麼添加到文件夾 瀏覽:239
四川麻將一般下哪個app 瀏覽:864
反編譯exe腳本 瀏覽:462
源碼文件夾怎麼編譯到固件中 瀏覽:912
ERp列印伺服器錯誤怎麼弄 瀏覽:113
蚌埠u盤加密軟體有哪些 瀏覽:180
前端如何認證伺服器 瀏覽:556
linux切換db2用戶命令 瀏覽:308
相片如何用電解壓 瀏覽:908
碩士程序員去學校當老師 瀏覽:122
pythonstr提取到字典 瀏覽:820
程序員那麼可愛有人看上陸漓了 瀏覽:878