我正在尝试从图库中选择一个图像到ImageView,但它无法正常工作。
以下是代码:
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");
}
}
});
}
这是布局:
<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>
这是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>
让我知道问题出在哪里,请:)我刚试了一些方法,但它从来没有做过工作
答案 0 :(得分:0)
您的代码中出现了错误:
String filePath = null;
因此您将空值传递给 DecodeFile 方法,因此位图为空。如果要将图像设置为ImageView,则可以在不将其转换为Bitmap的情况下执行此操作:
YourImageView.setImageURI(selectedImageUri);
答案 1 :(得分:0)
@Module
在这里拍照并从图库功能中选择:
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)
}
}
}
}
}