android從系統(tǒng)圖庫中取圖片的實例代碼
本文實例講述了android從系統(tǒng)圖庫中取圖片的實現(xiàn)方法。分享給大家供大家參考。具體如下:
在自己應用中,從系統(tǒng)圖庫中取圖片,然后截取其中一部分,再返回到自己應用中。這是很多有關(guān)圖片的應用需要的功能。
寫了一個示例,上來就是個大按鈕,連布局都不要了。最終,用選取圖片中的一部分作為按鈕的背景。
這里需要注意幾點:
① 從圖庫中選取出來保存的圖片剪輯,需要保存在sd卡目錄,不能保存在應用自己的在內(nèi)存的目錄,因為是系統(tǒng)圖庫來保存這個文件,它沒有訪問你應用的權(quán)限;
② intent.putExtra("crop", "true")才能出剪輯的小方框,不然沒有剪輯功能,只能選取圖片;
③ intent.putExtra("aspectX", 1),是剪輯方框的比例,可用于強制圖片的長寬比。
效果圖如下:
Java代碼如下:
package com.easymorse.gallery; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class GalleryActivity extends Activity { private static int SELECT_PICTURE; private File tempFile; Button button; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.tempFile=new File("/sdcard/a.jpg"); button = new Button(this); button.setText("獲取圖片"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra("crop", "true"); // intent.putExtra("aspectX", 1); // intent.putExtra("aspectY", 2); intent.putExtra("output", Uri.fromFile(tempFile)); intent.putExtra("outputFormat", "JPEG"); startActivityForResult(Intent.createChooser(intent, "選擇圖片"), SELECT_PICTURE); } }); setContentView(button); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { button.setBackgroundDrawable(Drawable.createFromPath(tempFile .getAbsolutePath())); } } } }
希望本文所述對大家的Android程序設計有所幫助。
- Android實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能
- Android實現(xiàn)保存圖片到本地并在相冊中顯示
- 基于Android實現(xiàn)保存圖片到本地并可以在相冊中顯示出來
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- Android畫圖并保存圖片的具體實現(xiàn)代碼
- Android實現(xiàn)調(diào)用系統(tǒng)圖庫與相機設置頭像并保存在本地及服務器
- Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
- android保存Bitmap圖片到指定文件夾示例
- Android截屏保存png圖片的實例代碼
- Android實現(xiàn)從網(wǎng)絡獲取圖片顯示并保存到SD卡的方法
- Android下保存簡單網(wǎng)頁到本地(包括簡單圖片鏈接轉(zhuǎn)換)實現(xiàn)代碼
- Android編程實現(xiàn)保存圖片到系統(tǒng)圖庫的方法示例
相關(guān)文章
LayoutAnimation給ListView中的item設置動態(tài)出場效果(實例)
下面小編就為大家?guī)硪黄狶ayoutAnimation給ListView中的item設置動態(tài)出場效果(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Android之內(nèi)置和外置sdcard路徑顯示并且寫入數(shù)據(jù)的方法
今天小編就為大家分享一篇Android之內(nèi)置和外置sdcard路徑顯示并且寫入數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)硪黄斦勛远xView之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android短信接收監(jiān)聽、自動回復短信操作例子
本文實現(xiàn)了短信接收監(jiān)聽,當接收到短信時,可自動回復短信,或自動回撥電話,同時監(jiān)聽短信的發(fā)送狀態(tài)2014-04-04Android編程基礎之簡單Button事件響應綜合提示控件Toast應用示例
這篇文章主要介紹了Android編程基礎之簡單Button事件響應綜合提示控件Toast應用,結(jié)合實例形式分析了Button事件響應與Toast提醒的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫
本篇文章主要介紹了詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫,具有一定的參考價值,有興趣的可以了解一下。2017-03-03Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
這篇文章主要為大家詳細介紹了Android SwipeRefreshLayout仿抖音app靜態(tài)刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03