[英]Picture from gallery to an imageview
i'm trying to select an image from gallery to an ImageView but it doesen't work.我正在尝试从图库中选择图像到 ImageView,但它不起作用。
Here's the code:这是代码:
public class Profilo extends MainActivity {
private ImageView img;
private Bitmap bmp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profilo);
img = (ImageView)findViewById(R.id.profile_image);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String filePath = null;
if (requestCode == 1) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
if (bmp != null && !bmp.isRecycled()) {
}
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
img.setBackgroundResource(0);
img.setImageBitmap(bmp);
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
});
}
Here's the layout:这是布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/profile_image"
android:contentDescription="@string/profile_image"
android:src="@drawable/profilo_facebook"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
and here's the MANIFEST:这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sidacri.testapp" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".Profilo"
android:label="@string/label_profilo">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Let me know where's the problem, please :) i've just tried some methods but it never doesen't work请让我知道问题出在哪里:) 我刚刚尝试了一些方法,但它从来没有不起作用
Got the bug in your code:在您的代码中发现错误:
String filePath = null;
so you are passing the null value to your DecodeFile method and so your Bitmap is null.因此您将空值传递给您的DecodeFile方法,因此您的位图为空。 If you want to set the image to ImageView,you can do that without converting it to Bitmap like this:
如果要将图像设置为 ImageView,则无需将其转换为 Bitmap 即可,如下所示:
YourImageView.setImageURI(selectedImageUri);
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data != null) {
when (requestCode) {
Constants.REQUEST_CODE_CAMERA -> {
val image: Bitmap = data.extras?.get("data") as Bitmap
binding.profilePhoto.setImageBitmap(image)
}
Constants.REQUEST_CODE_GALLERY -> {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P){
val source = ImageDecoder.createSource(
(activity as MainActivity).contentResolver,
data.data!!
)
val bitmap = ImageDecoder.decodeBitmap(source)
binding.profilePhoto.setImageBitmap(bitmap)
} else {
val bitmap = MediaStore.Images.Media.getBitmap((activity as MainActivity).contentResolver, data.data)
binding.profilePhoto.setImageBitmap(bitmap)
}
}
}
}
}
Here is take photo and pick from gallery functions:这是拍照并从画廊功能中挑选:
private fun openCamera() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, Constants.REQUEST_CODE_CAMERA)
}
private fun choosePhotoFromGallery() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, Constants.REQUEST_CODE_GALLERY)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.