Permalink
Browse files

Getting Images from the Gallery

  • Loading branch information...
dragosholban committed Mar 8, 2018
1 parent 002aea8 commit 572eca981e49fba8e9aa3bd164cedd9a7a823d40
@@ -42,6 +42,8 @@
String mCurrentPhotoPath;
private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 2;
private static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_PERMISSION_READ_EXTERNAL_STORAGE = 3;
static final int REQUEST_IMAGE_GALLERY = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -127,5 +129,22 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent.putExtra("mCurrentPhotoPath", mCurrentPhotoPath);
startActivity(intent);
}
if (requestCode == REQUEST_IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
Intent intent = new Intent(this, PuzzleActivity.class);
intent.putExtra("mCurrentPhotoUri", uri.toString());
startActivity(intent);
}
}
public void onImageFromGalleryClick(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_READ_EXTERNAL_STORAGE);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_IMAGE_GALLERY);
}
}
}
@@ -14,6 +14,7 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
@@ -31,6 +32,7 @@
public class PuzzleActivity extends AppCompatActivity {
ArrayList<PuzzlePiece> pieces;
String mCurrentPhotoPath;
String mCurrentPhotoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -43,6 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
final String assetName = intent.getStringExtra("assetName");
mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
mCurrentPhotoUri = intent.getStringExtra("mCurrentPhotoUri");
// run image related code after the view was laid out
// to have all dimensions calculated
@@ -53,6 +56,8 @@ public void run() {
setPicFromAsset(assetName, imageView);
} else if (mCurrentPhotoPath != null) {
setPicFromPath(mCurrentPhotoPath, imageView);
} else if (mCurrentPhotoUri != null) {
imageView.setImageURI(Uri.parse(mCurrentPhotoUri));
}
pieces = splitImage();
TouchListener touchListener = new TouchListener(PuzzleActivity.this);
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>
</vector>
@@ -38,4 +38,18 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/galleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:clickable="true"
android:onClick="onImageFromGalleryClick"
android:src="@drawable/ic_image_black_24dp"
android:tint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/cameraButton" />
</android.support.constraint.ConstraintLayout>

0 comments on commit 572eca9

Please sign in to comment.