新聞中心
在現(xiàn)代互聯(lián)網(wǎng)社會(huì)中,圖片已經(jīng)成為了網(wǎng)頁(yè)中不可或缺的重要元素,而數(shù)據(jù)庫(kù)中存儲(chǔ)的圖片則是通常在網(wǎng)頁(yè)中使用的圖片之一。然而,由于數(shù)據(jù)庫(kù)的數(shù)據(jù)存儲(chǔ)方式與普通文件存儲(chǔ)方式的不同,獲取數(shù)據(jù)庫(kù)中的圖片URL路徑相對(duì)來說會(huì)比較復(fù)雜。本文將介紹,從而在網(wǎng)頁(yè)中使用這些圖片。

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供周寧網(wǎng)站建設(shè)、周寧做網(wǎng)站、周寧網(wǎng)站設(shè)計(jì)、周寧網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、周寧企業(yè)網(wǎng)站模板建站服務(wù),10年周寧做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
一、了解數(shù)據(jù)庫(kù)中存儲(chǔ)的圖片類型
在數(shù)據(jù)庫(kù)中存儲(chǔ)圖片時(shí),我們需要了解圖片類型,以便于我們能夠更好地獲取圖片URL路徑,并在網(wǎng)頁(yè)中使用這些圖片。目前常用的圖片類型有兩種,一種是二進(jìn)制數(shù)據(jù)類型,另一種是BLOB類型。二進(jìn)制數(shù)據(jù)類型是指將圖片直接以二進(jìn)制數(shù)據(jù)的形式存儲(chǔ)在數(shù)據(jù)庫(kù)中,而BLOB類型則是在數(shù)據(jù)庫(kù)中存儲(chǔ)圖片的路徑。因此,當(dāng)我們要獲取數(shù)據(jù)庫(kù)中的圖片URL路徑時(shí),需要先判斷數(shù)據(jù)庫(kù)中存儲(chǔ)的圖片是哪種類型,然后根據(jù)不同類型來獲取圖片URL路徑。
二、獲取二進(jìn)制數(shù)據(jù)類型圖片的URL路徑
當(dāng)數(shù)據(jù)庫(kù)中存儲(chǔ)的圖片是二進(jìn)制數(shù)據(jù)類型時(shí),我們需要通過一系列步驟來獲取圖片URL路徑。我們需要通過SQL查詢將二進(jìn)制數(shù)據(jù)類型圖片從數(shù)據(jù)庫(kù)中讀取出來,然后將其寫入到一個(gè)本地圖片文件中。接著,我們需要通過服務(wù)器的文件系統(tǒng)訪問這個(gè)本地圖片文件。我們將該本地圖片文件的URL路徑返回給網(wǎng)頁(yè),這樣網(wǎng)頁(yè)就可以使用這個(gè)URL路徑來獲取圖片了。下面是實(shí)現(xiàn)這個(gè)過程的代碼示例:
“`python
import base64
import os
def get_image_url_from_binary_data(data):
# 將二進(jìn)制數(shù)據(jù)寫入到本地圖片文件中
with open(‘image.png’, ‘wb’) as f:
f.write(data)
# 獲取本地圖片文件的URL路徑
url = ‘http://localhost:8000/image.png’
return url
“`
三、獲取BLOB類型圖片的URL路徑
當(dāng)數(shù)據(jù)庫(kù)中存儲(chǔ)的圖片是BLOB類型時(shí),我們可以直接通過該圖片在服務(wù)器上存儲(chǔ)的路徑來獲取圖片URL路徑。具體而言,我們需要將該圖片的路徑以及服務(wù)器上圖片存儲(chǔ)的主機(jī)名和端口號(hào)拼接起來,形成完整的URL路徑。下面是實(shí)現(xiàn)這個(gè)過程的代碼示例:
“`python
def get_image_url_from_blob_data(data):
# 獲取服務(wù)器上存儲(chǔ)該圖片的路徑
path = ‘/path/to/image.png’
# 獲取服務(wù)器主機(jī)名和端口號(hào)
host = ‘localhost’
port = 8000
# 拼接完整的URL路徑
url = ‘http://{0}:{1}{2}’.format(host, port, path)
return url
“`
綜上所述,獲取數(shù)據(jù)庫(kù)中圖片的URL路徑需要根據(jù)不同的圖片類型采取不同的方法。如果是二進(jìn)制數(shù)據(jù)類型的圖片,則需要將其寫入到本地圖片文件中,然后獲取該本地圖片文件的URL路徑;如果是BLOB類型的圖片,則需要根據(jù)圖片在服務(wù)器上存儲(chǔ)的路徑以及服務(wù)器的主機(jī)名和端口號(hào)來拼接完整的URL路徑。獲取到這些URL路徑后,我們就可以在網(wǎng)頁(yè)中使用這些圖片了。
相關(guān)問題拓展閱讀:
- PHP如何讀取數(shù)據(jù)庫(kù)圖片路徑顯示出來,如何控制圖片的大小?
- android怎么從sd卡指定的文件夾中獲取所有圖片的路徑URL,謝謝~感謝各位大神了
PHP如何讀取數(shù)據(jù)庫(kù)圖片路徑顯示出來,如何控制圖片的大小?
取出數(shù)據(jù)庫(kù)保存的路徑$url
” width=”圖片寬度” height=”圖片高度”> 但要先構(gòu)架
圖片處理方案:
1首先上傳原圖片,
2使用GD創(chuàng)建圖片的縮略圖,
3將原圖和縮略圖文件的相對(duì)路徑存入數(shù)據(jù)庫(kù),
4使用圖片的時(shí)候?qū)⒙窂綇臄?shù)據(jù)庫(kù)中取出,就可以顯示在頁(yè)面了.
android怎么從sd卡指定的文件夾中獲取所有圖片的路徑URL,謝謝~感謝各位大神了
直接調(diào)用文件管理器選擇圖片即可。
1、調(diào)用系統(tǒng)提供的圖片選擇器,代碼如下:
//注意,在Android4.4系統(tǒng)下建議使用 Intent.ACTION_OPEN_DOCUMENT方式
if (Utility.isKK()) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(“image
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = “_data”;
final String projection = {
column
};
處理返回結(jié)果:
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case
case PIC_RESULT_KK:
imageFileUri = intent.getData();//獲取選擇圖片的URI
break;
}
}
2、除此自外,系統(tǒng)還提供一種選擇器,這個(gè)圖片選擇器可以屏蔽掉那個(gè)auto backup的目錄.所以就開始打算用這個(gè)圖片選擇器來選圖片了.
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(“image/jpeg”);
if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT){
startActivityForResult(intent, SELECT_PIC_KITKAT);
}else{
startActivityForResult(intent, SELECT_PIC);
}
為什么要分開不同版本呢?其實(shí)在4.3或以下可以直接用ACTION_GET_CONTENT的,在4.4或以上,官方建議用ACTION_OPEN_DOCUMENT,主要區(qū)別是他們返回的Uri.4.3返回的是帶文件路徑的,而4.4返回的卻是
參考:Android 4.4從圖庫(kù)選擇圖片,獲取圖片路徑并裁剪
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(“:”);
final String type = split;
if (“primary”.equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + “/” + split;
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse(” Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(“:”);
final String type = split;
Uri contentUri = null;
if (“image”.equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if (“video”.equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if (“audio”.equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = “_id=?”;
final String selectionArgs = new String {
split
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if (“content”.equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if (“file”.equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = “_data”;
final String projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return “com.android.externalstorage.documents”.equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return “com.android.providers.downloads.documents”.equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return “com.android.providers.media.documents”.equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return “com.google.android.apps.photos.content”.equals(uri.getAuthority());
}
關(guān)于數(shù)據(jù)庫(kù)url怎么獲取圖片路徑的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)建站主營(yíng):成都網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站改版的網(wǎng)站建設(shè)公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應(yīng)式移動(dòng)網(wǎng)站開發(fā)制作等網(wǎng)站服務(wù)。
本文標(biāo)題:如何獲取數(shù)據(jù)庫(kù)中圖片的URL路徑(數(shù)據(jù)庫(kù)url怎么獲取圖片路徑)
文章位置:http://fisionsoft.com.cn/article/djphcjo.html


咨詢
建站咨詢
