版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android如何實(shí)現(xiàn)相冊(cè)中圖片上傳或下載
這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)相冊(cè)中圖片上傳或下載的內(nèi)容。在下覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨在下過(guò)來(lái)看看吧。具體內(nèi)容如下目標(biāo)效果:打開(kāi)相冊(cè)選擇一張圖片,會(huì)顯示到上方的ImageView中并存儲(chǔ)到Bmob中,存儲(chǔ)后進(jìn)入Bmob后臺(tái),復(fù)制剛才添加的數(shù)據(jù)的objectId,粘貼到代碼指定出,然后運(yùn)行,點(diǎn)擊下載會(huì)在下方的ImageView顯示剛才上傳的圖片,這里的下載是指定objectId,可以進(jìn)行動(dòng)態(tài)獲取objectId進(jìn)行下載。1.activity_main.xml頁(yè)面設(shè)置布局。activity_main.xml頁(yè)面:<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/ivHead"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="150dp"
android:layout_height="150dp"/>
<Button
android:id="@+id/btnSelectImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ivHead"
android:layout_marginTop="16dp"
android:text="打開(kāi)相冊(cè)"
/>
<Button
android:id="@+id/btnDownloadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnSelectImage"
android:text="下載圖片"
/>
<ImageView
android:id="@+id/ivDownload"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btnDownloadImage"
/>
</RelativeLayout>2.MainActivity.java頁(yè)面定義上傳與下載方法。MainActivity.java頁(yè)面:package
com.example.text;
import
java.io.File;
import
cn.bmob.v3.Bmob;
import
cn.bmob.v3.BmobQuery;
import
cn.bmob.v3.datatype.BmobFile;
import
cn.bmob.v3.listener.DownloadFileListener;
import
cn.bmob.v3.listener.GetListener;
import
cn.bmob.v3.listener.UploadBatchListener;
import
cn.bmob.v3.listener.UploadFileListener;
import
com.android.volley.toolbox.ImageLoader;
import
com.example.text.MainActivity;
import
entity.Person;
import
.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
vider.MediaStore;
import
android.app.Activity;
import
android.content.Intent;
import
android.database.Cursor;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.drawable.BitmapDrawable;
import
android.graphics.drawable.Drawable;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
private
ImageView
ivHead,
ivDownload;
private
Button
btnSelectImage,
btnDownloadImage;
private
static
final
int
IMAGE_CODE
=
0;//
打開(kāi)相冊(cè)
private
static
final
int
RESIZE_CODE
=
2;//
調(diào)整大小
private
static
final
String
IMAGE_NAME
=
"
";//
圖片字符串
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bmob.initialize(this,
"8ef8a4752f48682eadb32d3c8c8e398f");//
初始化Bmob
//
初始化控件
init();
//
綁定點(diǎn)擊事件
bindClick();
}
//
初始化控件
private
void
init()
{
ivHead
=
(ImageView)
findViewById(R.id.ivHead);
ivDownload
=
(ImageView)
findViewById(R.id.ivDownload);
btnSelectImage
=
(Button)
findViewById(R.id.btnSelectImage);
btnDownloadImage
=
(Button)
findViewById(R.id.btnDownloadImage);
}
//
綁定點(diǎn)擊事件
private
void
bindClick()
{
btnSelectImage.setOnClickListener(this);
btnDownloadImage.setOnClickListener(this);
}
@Override
public
void
onClick(View
view)
{
switch
(view.getId())
{
case
R.id.btnSelectImage:
Intent
galleryIntent
=
new
Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
galleryIntent.setType("image/*");//圖片
startActivityForResult(galleryIntent,
IMAGE_CODE);
//跳轉(zhuǎn),傳遞打開(kāi)相冊(cè)請(qǐng)求碼
break;
case
R.id.btnDownloadImage:
BmobQuery<Person>
query=new
BmobQuery<Person>();
query.getObject(this,"ef292ff6ef",new
GetListener<Person>()
{
//第二個(gè)參數(shù)為想要下載的BmobFile數(shù)據(jù)的objectId
@Override
public
void
onSuccess(Person
person)
{
Toast.makeText(MainActivity.this,"獲取成功",Toast.LENGTH_SHORT).show();
//下載圖片
downloadImage(person);
}
@Override
public
void
onFailure(int
arg0,
String
arg1)
{
Toast.makeText(MainActivity.this,"獲取失敗",Toast.LENGTH_SHORT).show();
}
});
break;
default:
break;
}
}
@Override
protected
void
onActivityResult(int
requestCode,
int
resultCode,
Intent
data)
{
if
(resultCode
!=
RESULT_OK)
{
return;
}
else
{
switch
(requestCode)
{
case
IMAGE_CODE:
Uri
uri
=
data.getData();
resizeImage(uri);
//
將獲取到的uri轉(zhuǎn)換為String型
String[]
images
=
{
MediaStore.Images.Media.DATA
};//
將圖片URI轉(zhuǎn)換成存儲(chǔ)路徑
Cursor
cursor
=
this
.managedQuery(uri,
images,
null,
null,
null);
int
index
=
cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String
img_url
=
cursor.getString(index);
upload(img_url);
break;
case
RESIZE_CODE:
if
(data
!=
null)
{
showResizeImage(data);
}
break;
}
}
super.onActivityResult(requestCode,
resultCode,
data);
}
//
判斷SD卡是否存在
private
boolean
isSdcardExisting()
{
final
String
state
=
Environment.getExternalStorageState();
if
(state.equals(Environment.MEDIA_MOUNTED))
{
return
true;
}
else
{
return
false;
}
}
//
重塑圖片大小
public
void
resizeImage(Uri
uri)
{
Intent
intent
=
new
Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,
"image/*");
intent.putExtra("crop",
"true");//
可以裁剪
intent.putExtra("aspectX",
1);
intent.putExtra("aspectY",
1);
intent.putExtra("outputX",
150);
intent.putExtra("outputY",
150);
intent.putExtra("return-data",
true);
startActivityForResult(intent,
RESIZE_CODE);//
跳轉(zhuǎn),傳遞調(diào)整大小請(qǐng)求碼
}
//
獲取路徑
private
Uri
getImageUri()
{
return
Uri.fromFile(new
File(Environment.getExternalStorageDirectory(),
IMAGE_NAME));
}
//
顯示圖片
private
void
showResizeImage(Intent
data)
{
Bundle
extras
=
data.getExtras();
if
(extras
!=
null)
{
Bitmap
photo
=
extras.getParcelable("data");
Drawable
drawable
=
new
BitmapDrawable(photo);
ivHead.setImageDrawable(drawable);
}
}
//
圖片上傳
private
void
upload(String
imgpath)
{
final
BmobFile
icon
=
new
BmobFile(new
File(imgpath));
icon.upload(this,
new
UploadFileListener()
{
@Override
public
void
onSuccess()
{
Person
person
=
new
Person();
person.setIcon(icon);
person.save(MainActivity.this);
Toast.makeText(MainActivity.this,"圖片上傳成功",Toast.LENGTH_SHORT).show();
}
@Override
public
void
onFailure(int
arg0,
String
arg1)
{
Toast.makeText(MainActivity.this,"圖片上傳失敗",Toast.LENGTH_SHORT).show();
}
});
}
//下載圖片
private
void
downloadImage(Person
person)
{
BmobFile
icon=person.getIcon();
icon.download(MainActivity.this,new
DownloadFileListener()
{
@Override
public
void
onSuccess(String
url)
{
ivDownload.setImageBitmap(BitmapFactory.decodeFile(url));
//根據(jù)地址解碼并顯示圖片
}
@Override
public
void
onFailure(int
arg0,
String
arg1)
{
Toast.makeText(MainActivity.this,"下載失敗",Toast.LENGTH_SHORT).show();
}
});
}
}3.Person.java頁(yè)面為實(shí)體類(lèi),運(yùn)行自動(dòng)生成一個(gè)Person表,這個(gè)表只有一個(gè)字段存儲(chǔ)圖片。Person.java頁(yè)面:package
entity;
import
cn.bmob.v3.BmobObject;
import
cn.bmob.v3.datatype.BmobFile;
public
class
Person
extends
BmobObject{
private
BmobFile
icon;
public
BmobFile
getIcon()
{
return
icon;
}
public
void
setIcon(BmobFile
icon)
{
this.icon
=
icon;
}
}4.因?yàn)槭褂昧薆mob,所以需要添加權(quán)限。AndroidManifest.xml頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="/apk/res/android"
package="com.example.text"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<uses-permission
android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<!--
允許聯(lián)網(wǎng)
-->
<uses-permission
android:name="android.permission.INTERNET"
/>
<!--
獲取GSM(2g)、WCDMA(聯(lián)通3g)等網(wǎng)絡(luò)狀態(tài)的信息
-->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
/>
<!--
獲取wifi網(wǎng)絡(luò)狀態(tài)的信息
-->
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
/>
<!--
保持CPU
運(yùn)轉(zhuǎn),屏幕和鍵盤(pán)燈有可能是關(guān)閉的,用于文件上傳和下載
-->
<uses-permission
android:name="android.permission.WAKE_LOCK"
/>
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 黑龍江省哈爾濱市2025-2026學(xué)年六年級(jí)上學(xué)期期中語(yǔ)文試題(含答案)(含解析)
- 2026年員工敬業(yè)度調(diào)研分析技巧
- 2026黑龍江哈爾濱啟航勞務(wù)派遣有限公司派遣到哈工大航天學(xué)院衛(wèi)星技術(shù)研究所招聘?jìng)淇碱}庫(kù)及完整答案詳解1套
- 2026年農(nóng)村集體產(chǎn)權(quán)制度改革實(shí)務(wù)
- 機(jī)械設(shè)備液壓氣動(dòng)系統(tǒng)檢修手冊(cè)
- 2026湖南長(zhǎng)沙市長(zhǎng)郡雨花外國(guó)語(yǔ)第二附屬小學(xué)春季合同制教師招聘?jìng)淇碱}庫(kù)及答案詳解一套
- 2026年渠道經(jīng)銷(xiāo)商賦能管理培訓(xùn)
- 2026年私人銀行財(cái)富規(guī)劃策略課程
- 職業(yè)共病管理的分級(jí)診療路徑優(yōu)化
- 2022年年春六年級(jí)下冊(cè)數(shù)學(xué)期末測(cè)試卷加答案下載
- dbj41河南省城市地下綜合管廊施工與驗(yàn)收標(biāo)準(zhǔn)
- 2026屆新高考語(yǔ)文三輪沖刺復(fù)習(xí):二元思辨作文審題構(gòu)思寫(xiě)作
- 行業(yè)背景分析報(bào)告
- 2025中國(guó)農(nóng)業(yè)大學(xué)管理服務(wù)崗位(非事業(yè)編)招聘1人筆試備考試題附答案解析
- 2025福建省融資擔(dān)保有限責(zé)任公司招聘4人筆試試題附答案解析
- 工程管理費(fèi)合同協(xié)議
- 協(xié)助審計(jì)協(xié)議書(shū)范本
- GB/T 13471-2025節(jié)能項(xiàng)目經(jīng)濟(jì)效益計(jì)算與評(píng)價(jià)方法
- 2025年小學(xué)一年級(jí)語(yǔ)文拼音測(cè)試試卷(含答案)
- 電力公司安全第一課課件
- 2025年征兵心理模擬測(cè)試試題及答案
評(píng)論
0/150
提交評(píng)論