Skip to content

Commit

Permalink
Getting Images from the Gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosholban committed Mar 8, 2018
1 parent 002aea8 commit 572eca9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
Expand Up @@ -42,6 +42,8 @@ public class MainActivity extends AppCompatActivity {
String mCurrentPhotoPath; String mCurrentPhotoPath;
private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 2; private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 2;
private static final int REQUEST_IMAGE_CAPTURE = 1; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -127,5 +129,22 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent.putExtra("mCurrentPhotoPath", mCurrentPhotoPath); intent.putExtra("mCurrentPhotoPath", mCurrentPhotoPath);
startActivity(intent); 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);
}
} }
} }
Expand Up @@ -14,6 +14,7 @@
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.ExifInterface; import android.media.ExifInterface;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.ImageView; import android.widget.ImageView;
Expand All @@ -31,6 +32,7 @@
public class PuzzleActivity extends AppCompatActivity { public class PuzzleActivity extends AppCompatActivity {
ArrayList<PuzzlePiece> pieces; ArrayList<PuzzlePiece> pieces;
String mCurrentPhotoPath; String mCurrentPhotoPath;
String mCurrentPhotoUri;


@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -43,6 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent(); Intent intent = getIntent();
final String assetName = intent.getStringExtra("assetName"); final String assetName = intent.getStringExtra("assetName");
mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath"); mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
mCurrentPhotoUri = intent.getStringExtra("mCurrentPhotoUri");


// run image related code after the view was laid out // run image related code after the view was laid out
// to have all dimensions calculated // to have all dimensions calculated
Expand All @@ -53,6 +56,8 @@ public void run() {
setPicFromAsset(assetName, imageView); setPicFromAsset(assetName, imageView);
} else if (mCurrentPhotoPath != null) { } else if (mCurrentPhotoPath != null) {
setPicFromPath(mCurrentPhotoPath, imageView); setPicFromPath(mCurrentPhotoPath, imageView);
} else if (mCurrentPhotoUri != null) {
imageView.setImageURI(Uri.parse(mCurrentPhotoUri));
} }
pieces = splitImage(); pieces = splitImage();
TouchListener touchListener = new TouchListener(PuzzleActivity.this); TouchListener touchListener = new TouchListener(PuzzleActivity.this);
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_image_black_24dp.xml
@@ -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>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -38,4 +38,18 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="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> </android.support.constraint.ConstraintLayout>

0 comments on commit 572eca9

Please sign in to comment.