Skip to content

Commit

Permalink
Fixes #446 Now photos can be selected using google photos
Browse files Browse the repository at this point in the history
  • Loading branch information
anantprsd5 committed Feb 4, 2017
1 parent c3f5d11 commit 757e1d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:name=".MainApplication"
Expand Down
Expand Up @@ -84,6 +84,7 @@

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -328,16 +329,14 @@ public void run() {
InputStream imageStream;
Bitmap selectedImage;
try {
cropCapturedImage(selectedImageUri);
cropCapturedImage(getImageUrl(getApplicationContext(), selectedImageUri));
} catch (ActivityNotFoundException aNFE) {
//display an error message if user device doesn't support
showToast(getString(R.string.error_crop_not_supported));
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
Cursor cursor = getContentResolver().query(getImageUrl(getApplicationContext(), selectedImageUri), filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
imageStream = getContentResolver().openInputStream(selectedImageUri);
selectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand All @@ -361,6 +360,31 @@ public void run() {
}
}

public static Uri writeToTempImage(Context inContext, Bitmap inImage) {
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

public static Uri getImageUrl(Context context, Uri uri) {
InputStream is = null;
if (uri.getAuthority() != null) {
try {
is = context.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is);
return writeToTempImage(context, bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

private void init() {
ButterKnife.bind(this);
realm = Realm.getDefaultInstance();
Expand Down Expand Up @@ -1348,6 +1372,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case R.id.wall_settings:
chatBackgroundActivity();
int permissionCheck = ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
return true;
case R.id.action_share:
try {
Expand Down

0 comments on commit 757e1d4

Please sign in to comment.