Skip to content

Instantly share code, notes, and snippets.

@ssd863419
Created October 10, 2014 09:00
Show Gist options
  • Save ssd863419/72ad5445713e95a508f4 to your computer and use it in GitHub Desktop.
Save ssd863419/72ad5445713e95a508f4 to your computer and use it in GitHub Desktop.
Android 取得手機內的圖片, Intent, Intent.ACTION_GET_CONTENT, Uri, Log, ContentResolver, Bitmap, startActivityForResult
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取得圖片"
android:id="@+id/myButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myImage" />
</LinearLayout>
package com.example.administrator.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileNotFoundException;
/**
* Created by Administrator on 2014/10/10.
*/
public class GetPictureDemo extends Activity {
private Button mButton;
private ImageView mImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getpicture);
mButton = (Button) findViewById(R.id.myButton);
mImageView = (ImageView) findViewById(R.id.myImage);
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*"); //開啟Pictures畫面Type設定為image
intent.setAction(Intent.ACTION_GET_CONTENT); //使用Intent.ACTION_GET_CONTENT
startActivityForResult(intent, 1); //取得相片後, 返回
}
});
}
/* 取得相片後返回的監聽 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/* 當使用者按下確定後 */
if (resultCode == RESULT_OK) {
Uri uri = data.getData(); //取得圖檔的路徑
Log.e("uri", uri.toString()); //寫log
ContentResolver cr = this.getContentResolver(); //抽象資料的接口
try {
/* 由抽象資料接口轉換圖檔路徑為Bitmap */
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
mImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
呼叫android取得手機內圖片很簡單,只要用Intent.ACTION_GET_CONTENT
重點在於取得圖檔後面的處理,因為取得只是取出該圖檔的存放路徑,所以必須經由
抽象接口ContentResolver再把該圖檔轉換為Bitmap,轉換為Bitmap主要是用來餵給imageView
下面是幾種常用可以設定imageView內圖片的型式
imageView.setImageBitmap(bitmap) //bitmap圖檔型式
imageView.setImageDrawable(drawable) //drawable圖檔型式
imageView.setImageResource(resId) //存在應用程式中res/drawable內的圖片
imageView.setImageURI(uri)//網路圖片路徑
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment