導航:首頁 > 操作系統 > 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訪問本地文件的文件相關的資料

熱點內容
cad常見的快捷命令 瀏覽:625
伺服器端有什麼 瀏覽:325
文件夾正在使用如何重命名 瀏覽:378
單片機觸摸 瀏覽:875
qq收藏夾在手機哪個文件夾 瀏覽:755
為什麼app的密碼總是不正確 瀏覽:324
方舟手機版為什麼進不了伺服器 瀏覽:594
伺服器ip可以查到真實地址嗎 瀏覽:656
象棋軟體演算法 瀏覽:993
飄零加密 瀏覽:175
文件加密軟體哪個好用免費保險櫃 瀏覽:752
黑石物理伺服器是雲伺服器嗎 瀏覽:621
java讀文件一行 瀏覽:793
熔火之心伺服器是什麼 瀏覽:628
湯子瀛第四版pdf 瀏覽:827
剛剛解壓的車能過戶嗎 瀏覽:523
pdf證書加密開發 瀏覽:159
android緩存工具類 瀏覽:220
pic單片機秒錶 瀏覽:632
源代碼如何放到伺服器 瀏覽:73