Skip to content

Commit 572eca9

Browse files
committed
Getting Images from the Gallery
1 parent 002aea8 commit 572eca9

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

app/src/main/java/dragosholban/com/androidpuzzlegame/MainActivity.java

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class MainActivity extends AppCompatActivity {
4242
String mCurrentPhotoPath;
4343
private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 2;
4444
private static final int REQUEST_IMAGE_CAPTURE = 1;
45+
static final int REQUEST_PERMISSION_READ_EXTERNAL_STORAGE = 3;
46+
static final int REQUEST_IMAGE_GALLERY = 4;
4547

4648
@Override
4749
protected void onCreate(Bundle savedInstanceState) {
@@ -127,5 +129,22 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
127129
intent.putExtra("mCurrentPhotoPath", mCurrentPhotoPath);
128130
startActivity(intent);
129131
}
132+
if (requestCode == REQUEST_IMAGE_GALLERY && resultCode == RESULT_OK) {
133+
Uri uri = data.getData();
134+
135+
Intent intent = new Intent(this, PuzzleActivity.class);
136+
intent.putExtra("mCurrentPhotoUri", uri.toString());
137+
startActivity(intent);
138+
}
139+
}
140+
141+
public void onImageFromGalleryClick(View view) {
142+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
143+
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_READ_EXTERNAL_STORAGE);
144+
} else {
145+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
146+
intent.setType("image/*");
147+
startActivityForResult(intent, REQUEST_IMAGE_GALLERY);
148+
}
130149
}
131150
}

app/src/main/java/dragosholban/com/androidpuzzlegame/PuzzleActivity.java

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.graphics.drawable.BitmapDrawable;
1515
import android.graphics.drawable.Drawable;
1616
import android.media.ExifInterface;
17+
import android.net.Uri;
1718
import android.support.v7.app.AppCompatActivity;
1819
import android.os.Bundle;
1920
import android.widget.ImageView;
@@ -31,6 +32,7 @@
3132
public class PuzzleActivity extends AppCompatActivity {
3233
ArrayList<PuzzlePiece> pieces;
3334
String mCurrentPhotoPath;
35+
String mCurrentPhotoUri;
3436

3537
@Override
3638
protected void onCreate(Bundle savedInstanceState) {
@@ -43,6 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
4345
Intent intent = getIntent();
4446
final String assetName = intent.getStringExtra("assetName");
4547
mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
48+
mCurrentPhotoUri = intent.getStringExtra("mCurrentPhotoUri");
4649

4750
// run image related code after the view was laid out
4851
// to have all dimensions calculated
@@ -53,6 +56,8 @@ public void run() {
5356
setPicFromAsset(assetName, imageView);
5457
} else if (mCurrentPhotoPath != null) {
5558
setPicFromPath(mCurrentPhotoPath, imageView);
59+
} else if (mCurrentPhotoUri != null) {
60+
imageView.setImageURI(Uri.parse(mCurrentPhotoUri));
5661
}
5762
pieces = splitImage();
5863
TouchListener touchListener = new TouchListener(PuzzleActivity.this);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
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"/>
9+
</vector>

app/src/main/res/layout/activity_main.xml

+14
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@
3838
app:layout_constraintBottom_toBottomOf="parent"
3939
app:layout_constraintEnd_toEndOf="parent" />
4040

41+
<android.support.design.widget.FloatingActionButton
42+
android:id="@+id/galleryButton"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:layout_marginBottom="16dp"
46+
android:layout_marginEnd="8dp"
47+
android:layout_marginRight="8dp"
48+
android:clickable="true"
49+
android:onClick="onImageFromGalleryClick"
50+
android:src="@drawable/ic_image_black_24dp"
51+
android:tint="@android:color/white"
52+
app:layout_constraintBottom_toBottomOf="parent"
53+
app:layout_constraintEnd_toStartOf="@id/cameraButton" />
54+
4155
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)