Android開發(fā)從相機(jī)或相冊(cè)獲取圖片裁剪
廢話不多說(shuō)了,直接給大家貼代碼了。
package com.only.android.app; import java.io.File; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.SystemClock; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.only.android.R; public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener { /** Called when the activity is first created. */ private Button selectImageBtn; private ImageView imageView; private File sdcardTempFile; private AlertDialog dialog; private int crop = 180; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imagescale); selectImageBtn = (Button) findViewById(R.id.selectImageBtn); imageView = (ImageView) findViewById(R.id.imageView); selectImageBtn.setOnClickListener(this); sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg"); } @Override public void onClick(View v) { if (v == selectImageBtn) { if (dialog == null) { dialog = new AlertDialog.Builder(this).setItems(new String[] { "相機(jī)", "相冊(cè)" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == 0) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra("output", Uri.fromFile(sdcardTempFile)); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1);// 裁剪框比例 intent.putExtra("aspectY", 1); intent.putExtra("outputX", crop);// 輸出圖片大小 intent.putExtra("outputY", crop); startActivityForResult(intent, 101); } else { Intent intent = new Intent("android.intent.action.PICK"); intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); intent.putExtra("output", Uri.fromFile(sdcardTempFile)); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1);// 裁剪框比例 intent.putExtra("aspectY", 1); intent.putExtra("outputX", crop);// 輸出圖片大小 intent.putExtra("outputY", crop); startActivityForResult(intent, 100); } } }).create(); } if (!dialog.isShowing()) { dialog.show(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == RESULT_OK) { Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath()); imageView.setImageBitmap(bmp); } } }
以上代碼很簡(jiǎn)單,相信大家都可以看的懂吧,欲了解更多信息請(qǐng)持續(xù)關(guān)注本站,謝謝。
- Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
- Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)相機(jī)拍攝、選擇、圖片裁剪功能
- 使用Java代碼在Android中實(shí)現(xiàn)圖片裁剪功能
- android實(shí)現(xiàn)圖片裁剪的兩種方法
相關(guān)文章
Android開發(fā)應(yīng)用第一步 安裝及配置模擬器Genymotion
這篇文章主要介紹了Android開發(fā)應(yīng)用第一步,即安裝及配置模擬器Genymotion,感興趣的小伙伴們可以參考一下2015-12-12Arduino 數(shù)據(jù)類型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹
這篇文章主要介紹了Arduino 數(shù)據(jù)類型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Android使用lottie加載json動(dòng)畫的示例代碼
本篇文章主要介紹了Android使用lottie加載json動(dòng)畫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-01-01Kotlin使用協(xié)程實(shí)現(xiàn)高效并發(fā)程序流程詳解
這篇文章主要介紹了Kotlin使用協(xié)程實(shí)現(xiàn)高效并發(fā)程序流程,協(xié)程屬于Kotlin中非常有特色的一項(xiàng)技術(shù),因?yàn)榇蟛糠志幊陶Z(yǔ)言中是沒有協(xié)程這個(gè)概念的。那么什么是協(xié)程呢?它其實(shí)和線程有點(diǎn)相似,可以簡(jiǎn)單地將它理解成一種輕量級(jí)的線程2023-01-01Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能
這篇文章主要介紹了Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能的相關(guān)資料,需要的朋友可以參考下2016-02-02Android中的深度鏈接技術(shù)實(shí)戰(zhàn)
本文主要介紹了Android中的深度鏈接技術(shù)實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Kotlin 擴(kuò)展函數(shù)和擴(kuò)展屬性的使用方法
這篇文章主要介紹了Kotlin 擴(kuò)展函數(shù)和擴(kuò)展屬性的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10RecyclerView+PagerSnapHelper實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)的Viewpager效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView+PagerSnapHelper實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)的Viewpager效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10