I want to load a Picture from my gallery and display it in my app. I tried ImageBitmap
, and ImageURI
but i just doesn't display the picture.
Here is a part of my java code:
private ListView LIST_Photos;
private TextView TV_SessionName, TV_SessionInfo;
private ImageView IV_Picture;
private ArrayList<HashMap<String, String>> photos;
private Session session;
private static int RESULT_LOAD_IMAGE = 1;
private ImageView imageView;
public void addPhotograph(View view){
/*Intent intent = new Intent(this, NewPhotographActivity.class);
startActivity(intent);//*/
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
String picturePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
cursor.close();
File imgFile = new File(picturePath);
imageView = (ImageView) findViewById(R.id.IV);
imageView.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
imageView.setImageURI(selectedImage);
imageView.setImageURI(Uri.fromFile(imgFile));
}
}
And here is part of the xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/IV"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/avatars[0]" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/BT_addPhotograph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:onClick="addPhotograph"
app:borderWidth="0dp"
app:elevation="0dp"
app:hoveredFocusedTranslationZ="0dp"
app:maxImageSize="42dp"
app:pressedTranslationZ="0dp"
app:srcCompat="@drawable/add" />
</FrameLayout>
This is everything i found online and i even tried copying a functional app but it doesn't work either. I Don't get any Runtime or Logcat errors and the permissions are all accepted.
Use bitmap you can set Image directly by using Bitmap
public void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = findViewById(R.id.IV);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.