Android實(shí)現(xiàn)相冊(cè)中圖片上傳或下載
本文實(shí)例為大家分享了Android實(shí)現(xiàn)相冊(cè)中圖片上傳或下載的具體代碼,供大家參考,具體內(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="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/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 android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.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í)體類,運(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="http://schemas.android.com/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),屏幕和鍵盤燈有可能是關(guān)閉的,用于文件上傳和下載 --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- 獲取sd卡寫的權(quán)限,用于文件上傳和下載 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 允許讀取手機(jī)狀態(tài) 用于創(chuàng)建BmobInstallation --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.text.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
5.注意之前講過(guò)使用Bmob需要下載第三方SDK,將libs文件夾中的所有內(nèi)容都復(fù)制到項(xiàng)目libs目錄下,然后Properties->Java Build Path->Libraries->Add JARs...添加剛復(fù)制的四個(gè)jar包
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio實(shí)現(xiàn)仿微信APP門戶界面詳解及源碼
這篇文章帶你通過(guò)Android studio來(lái)實(shí)現(xiàn)微信APP的門戶界面,主要說(shuō)明框架的各部分功能與實(shí)現(xiàn)過(guò)程,下文包含了整個(gè)開(kāi)發(fā)過(guò)程,以及解決問(wèn)題的思路并再末尾提供了源碼鏈接2021-10-10android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
實(shí)現(xiàn)倒計(jì)時(shí)每隔1秒,變換一下時(shí)間,截圖如下,感興趣的朋友想看下實(shí)現(xiàn)代碼,希望對(duì)你學(xué)習(xí)有所幫助2013-06-06Android Flutter實(shí)現(xiàn)上拉加載組件的示例代碼
既然列表有下拉刷新外當(dāng)然還有上拉加載更多操作了,本次就為大家詳細(xì)介紹如何利用Flutter實(shí)現(xiàn)為列表增加上拉加載更多的交互,感興趣的可以了解一下2022-08-08android開(kāi)發(fā)教程之textview內(nèi)容超出屏幕寬度顯示省略號(hào)
android開(kāi)發(fā)中用textview顯示內(nèi)容時(shí),顯示內(nèi)容過(guò)多可能會(huì)折行或顯示不全,那樣效果很不好,我們可以用省略號(hào)顯示,下面看設(shè)置方法2014-02-02Android App中使用ListFragment的實(shí)例教程
這篇文章主要介紹了Android App中使用ListFragment的實(shí)例教程,ListFragment的內(nèi)容是以列表(list)的形式顯示的Fragment,需要的朋友可以參考下2016-05-05Android異常 java.lang.IllegalStateException解決方法
這篇文章主要介紹了Android異常 java.lang.IllegalStateException解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android編程中context及全局變量實(shí)例詳解
這篇文章主要介紹了Android編程中context及全局變量的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析講述了context及全局變量的使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-12-12Android React Native原生模塊與JS模塊通信的方法總結(jié)
這篇文章主要介紹了Android React Native原生模塊與JS模塊通信的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-02-02Android中關(guān)于CoordinatorLayout的一些實(shí)用布局技巧
大家都知道CoordinatorLayout是一個(gè)“加強(qiáng)版”的 FrameLayout,那么下面這篇文章主要給大家分享了Android中關(guān)于CoordinatorLayout的一些布局技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06Android Flutter實(shí)現(xiàn)五種酷炫文字動(dòng)畫效果詳解
animated_text_kit這一動(dòng)畫庫(kù)有多種文字動(dòng)畫效果,文中將利用它實(shí)現(xiàn)五種酷炫的文字動(dòng)畫:波浪涌動(dòng)效果、波浪線跳動(dòng)文字組、彩虹動(dòng)效、滾動(dòng)廣告牌效果和打字效果,需要的可以參考一下2022-03-03