日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 实现打开相册

發布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 实现打开相册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近在學習安卓的過程中,學到了從相冊中選取圖片的內容。 // java版 Activity的代碼 import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;import java.io.FileNotFoundException; public class MainActivity extends AppCompatActivity {int REQUEST_IMAGE_OPEN = 2;ImageView imgView = null; //用于顯示圖片@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);//打開相冊按鈕imgView = (ImageView) findViewById(R.id.imgView);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//打開相冊Intent intent = new Intent(Intent.ACTION_PICK);//指定獲取的是圖片intent.setType("image/*");startActivityForResult(intent, REQUEST_IMAGE_OPEN);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);//將選擇的圖片顯示if(requestCode == REQUEST_IMAGE_OPEN && resultCode == Activity.RESULT_OK &&data !=null){Uri uris;uris = data.getData();Bitmap bitmap = null;//Uri轉化為Bitmaptry {bitmap = getBitmapFromUri(uris);} catch (FileNotFoundException e) {e.printStackTrace();}imgView.setImageBitmap(bitmap);}}//Uri轉化為Bitmapprivate Bitmap getBitmapFromUri(Uri uri) throws FileNotFoundException {Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));return bitmap;} } // kotlin版 Activity的代碼 import android.app.Activity import android.content.Intent import android.graphics.BitmapFactory import android.net.Uri import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {val REQUEST_IMAGE_OPEN = 2override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)button.setOnClickListener {val intent = Intent(Intent.ACTION_PICK)//指定獲取的是圖片intent.type = "image/*"startActivityForResult(intent, REQUEST_IMAGE_OPEN);}}override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)when(requestCode){REQUEST_IMAGE_OPEN->{if(resultCode == Activity.RESULT_OK &&data !=null){data.data?.let{ uri->//將圖片顯示val bitmap = getBitmapFromUri(uri)imgView.setImageBitmap(bitmap)}}}}}private fun getBitmapFromUri(uri: Uri) = contentResolver.openFileDescriptor(uri,"r")?.use {BitmapFactory.decodeFileDescriptor(it.fileDescriptor)} }

官方文檔用的是Intent.ACTION_OPEN_DOCUMENT 或者Intent.ACTION_GET_CONTENT

這里用了Intent.ACTION_PICK實現獲取圖片

總結

以上是生活随笔為你收集整理的android 实现打开相册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。