當前位置:首頁 » 安卓系統 » android讀取圖片並顯示

android讀取圖片並顯示

發布時間: 2025-02-16 07:57:42

1. android 存在資料庫中的動態圖片,如何讀取出來,顯示在ImageView中

實現的功能為從伺服器獲取圖片數據,在布局頁面上顯示。由於圖片的個數是不確定的,因此採用在布局頁面中定義多個ImageView來顯示圖片是不合理的。
(一)首先定義布局

android:id="@+id/id_layout_movie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
(二)載入圖片顯示時獲取到布局文件
RelativeLayout rl_Movie = (RelativeLayout) findViewById(R.id.id_layout_movie);
(三)依次循環伺服器獲取的圖片數據,一張一張設置圖片顯示的位置
//newWidth為圖片顯示的寬度,newHeight為圖片顯示的高度
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams( newWidth, newHeight);
設置lp1.leftMargin和lp1.topMargin的值
(四)最後設置rl_Movie.addView(iv, lp1)將圖片加入布局文件中

2. android如何讀取sd卡的圖片並顯示

首先你要在AndroidManifest.xml申請讀取sdcard的許可權

java"><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!--向SDCard寫入數據許可權-->

關鍵代碼:

packagecom.sdcardread;

importjava.io.File;

importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;

{
privateTextViewtextView1;
;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
linearLayout1=(LinearLayout)findViewById(R.id.linearLayout1);
booleanisSdCardExist=Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);//判斷sdcard是否存在
if(isSdCardExist){
Stringsdpath=Environment.getExternalStorageDirectory()
.getAbsolutePath();//獲取sdcard的根路徑
textView1.setText("sd卡是存在的。以下是sdcard下的img25.jpg!");
Stringfilepath=sdpath+File.separator+"img25.jpg";
Filefile=newFile(filepath);
ImageViewimageView=newImageView(this);//創建一個imageView對象
if(file.exists()){
Bitmapbm=BitmapFactory.decodeFile(filepath);
//將圖片顯示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
}else{
textView1.setText("sd卡不存在!");
}

}

}

3. android 網路獲取圖片並在activity上顯示

在Android應用中,獲取網路圖片並在Activity中顯示,是一個常見的需求。首先,你需要使用HttpURLConnection或OkHttp等網路庫來下載圖片。這里,我們可以使用OkHttp,因為它提供了簡潔且高效的API。

以下是一個簡單的示例代碼,用於從網路獲取圖片並將其設置為Activity的背景:

1. 添加依賴

在項目級build.gradle文件中添加OkHttp依賴:

implementation 'com.squareup.okhttp3:okhttp:4.9.0'

2. 獲取網路圖片

使用OkHttp發起網路請求獲取圖片。這里我們使用一個簡單的GET請求:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("https://example.com/image.png").build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
byte[] bytes = response.body().bytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}

3. 設置圖片為Activity背景

獲取到圖片後,可以將其設置為Activity的背景。我們可以在Activity的onCreate方法中完成這一操作:

ImageView imageView = findViewById(R.id.background_image);
imageView.setImageBitmap(bitmap);
View backgroundView = findViewById(R.id.activity_background);
backgroundView.setBackground(new BitmapDrawable(getResources(), bitmap));

以上步驟展示了如何從網路獲取圖片並在Android應用的Activity中顯示。值得注意的是,為了提升用戶體驗,應當確保網路請求非同步執行,避免阻塞主線程。

此外,在實際應用中,還需要處理可能出現的異常情況,比如網路請求失敗或圖片下載失敗等。可以使用try-catch語句來捕獲並處理這些異常。

通過這種方式,我們可以在Android應用中輕松實現從網路獲取圖片並在Activity中展示的功能。

熱點內容
為什麼人買一個蘋果一個安卓 發布:2025-03-17 13:36:59 瀏覽:438
三星手機簡訊在那個文件夾 發布:2025-03-17 13:31:51 瀏覽:194
安卓皇帝隱藏劇情在哪裡 發布:2025-03-17 13:18:53 瀏覽:507
新版安卓為什麼不兼容 發布:2025-03-17 13:18:49 瀏覽:483
s3哪個配置性價比高 發布:2025-03-17 13:06:09 瀏覽:320
氣體壓縮能量 發布:2025-03-17 13:00:16 瀏覽:78
壓縮油19 發布:2025-03-17 12:25:29 瀏覽:858
linux上網代理 發布:2025-03-17 12:23:56 瀏覽:361
c是高級語言嗎 發布:2025-03-17 12:16:31 瀏覽:525
python泛型 發布:2025-03-17 12:15:01 瀏覽:484