Skip to content

Instantly share code, notes, and snippets.

@achimoraites
Created February 24, 2018 16:14
Show Gist options
  • Save achimoraites/7c8b41f46dff70d4ff0e7b55ffc9dd3e to your computer and use it in GitHub Desktop.
Save achimoraites/7c8b41f46dff70d4ff0e7b55ffc9dd3e to your computer and use it in GitHub Desktop.
Sample code for a simple image picker for android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
tools:context="io.github.cyb3rn4u7.imagepicker.MainActivity">
<ImageView
android:id="@+id/display_image"
android:layout_width="256dp"
android:layout_height="256dp"
android:src="@drawable/ic_photo_size_select"
android:layout_centerInParent="true"
/>
<Button
android:id="@+id/pick_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select image"
android:drawableTop="@drawable/ic_insert_photo"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.cyb3rn4u7.imagepicker">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package io.github.cyb3rn4u7.imagepicker;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int IMG_RESULT = 101 ;
private ImageView imageView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.display_image);
button = findViewById(R.id.pick_img);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == IMG_RESULT && resultCode == RESULT_OK
&& null != data) {
// get the selected image
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
// display the image
imageView.setImageBitmap(bitmap);
}
} catch (Exception e) {
Toast.makeText(this,"oops something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment