Мне удалось получить доступ к моей галерее Android после нажатия кнопки и выбора изображения, но в imageView ничего не отображается.
вот файл main.xml:
Код: Выделить всё
вот мой Java-код:
Код: Выделить всё
public class ImagesProj extends Activity {
// define the variable that will show the gallery images
ImageView imageViewGallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images_proj);
// connect the variable to the images_proj.xml
imageViewGallery = (ImageView) findViewById(R.id.imageViewGallery);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
// This function is called after clicking the Browse From Gallery button
public boolean onButtonClicked(View view) {
// access the images gallery
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && 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();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// String picturePath contains the path of selected Image
// Show the Selected Image on ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageViewGallery);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
сообщение, которое я пытался просмотреть:
Выбор фотографии из галереи и отображение в виде изображения
Подробнее здесь: https://stackoverflow.com/questions/291 ... id-gallery
Мобильная версия