欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android從系統(tǒng)圖庫中取圖片的實例代碼

 更新時間:2015年07月21日 10:01:57   作者:華宰  
這篇文章主要介紹了android從系統(tǒng)圖庫中取圖片的方法,涉及Android讀取及選擇圖片等相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了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程序設計有所幫助。

相關(guān)文章

最新評論