android 实现打开相册
生活随笔
收集整理的這篇文章主要介紹了
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 实现打开相册的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 征信管理条例不良信息保存期限
- 下一篇: 好退休军人参观奥体写什么横幅好?