Created
October 10, 2014 09:16
-
-
Save ssd863419/117262742c1009da3822 to your computer and use it in GitHub Desktop.
Android 取得裁剪後的圖片, File, Intent, Intent.ACTION_GET_CONTENT, startActivityForResult, setImageDrawable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.administrator.test; | |
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.widget.Button; | |
import android.widget.ImageView; | |
import java.io.File; | |
/** | |
* Created by Administrator on 2014/10/10. | |
*/ | |
public class GetPictureDemo extends Activity { | |
private Button mButton; | |
private ImageView mImageView; | |
private File tempFile; //用來臨時存放裁剪後的圖片 | |
@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); | |
tempFile = new File("/sdcard/a.jpg"); //這句一定要宣告在onCreate()裡面 | |
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 | |
intent.putExtra("crop", "true"); //crop = true, 才會出現裁剪 | |
/* aspectX, aspectY, 為裁剪框的比例, x:y = 1:1 */ | |
intent.putExtra("aspectX", 1); | |
intent.putExtra("aspectY", 1); | |
intent.putExtra("output", Uri.fromFile(tempFile)); | |
/* 取得圖片後, 返回 */ | |
startActivityForResult(intent.createChooser(intent, "選擇圖片"), 1); | |
} | |
}); | |
} | |
/* 取得相片後返回的監聽 */ | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
/* 當使用者按下確定後 */ | |
if (resultCode == RESULT_OK) { | |
mImageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath())); | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment