Fix Crashing on profile/banner selecting

This commit is contained in:
h4h13 2019-07-11 00:37:21 +05:30
parent 048c4f6cef
commit 00f33cab3e
5 changed files with 37 additions and 83 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,18 +1,20 @@
package code.name.monkey.retromusic.activities package code.name.monkey.retromusic.activities
import android.app.Activity import android.app.Activity
import android.content.* import android.content.ContentUris
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.provider.MediaStore import android.provider.MediaStore.Images.Media
import android.provider.MediaStore.Images.Media.getBitmap import android.provider.MediaStore.Images.Media.getBitmap
import android.text.TextUtils import android.text.TextUtils
import android.view.MenuItem import android.view.MenuItem
import android.widget.Toast import android.widget.Toast
import androidx.core.content.FileProvider
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.ColorUtil import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialUtil import code.name.monkey.appthemehelper.util.MaterialUtil
@ -56,10 +58,10 @@ class UserInfoActivity : AbsBaseActivity() {
name.setText(PreferenceUtil.getInstance().userName) name.setText(PreferenceUtil.getInstance().userName)
bio.setText(PreferenceUtil.getInstance().userBio) bio.setText(PreferenceUtil.getInstance().userBio)
if (!PreferenceUtil.getInstance().profileImage.isEmpty()) { if (PreferenceUtil.getInstance().profileImage.isNotEmpty()) {
loadImageFromStorage(PreferenceUtil.getInstance().profileImage) loadImageFromStorage(PreferenceUtil.getInstance().profileImage)
} }
if (!PreferenceUtil.getInstance().bannerImage.isEmpty()) { if (PreferenceUtil.getInstance().bannerImage.isNotEmpty()) {
loadBannerFromStorage(PreferenceUtil.getInstance().bannerImage) loadBannerFromStorage(PreferenceUtil.getInstance().bannerImage)
} }
userImage.setOnClickListener { userImage.setOnClickListener {
@ -129,7 +131,7 @@ class UserInfoActivity : AbsBaseActivity() {
private fun selectBannerImage() { private fun selectBannerImage() {
if (TextUtils.isEmpty(PreferenceUtil.getInstance().bannerImage)) { if (TextUtils.isEmpty(PreferenceUtil.getInstance().bannerImage)) {
val pickImageIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) val pickImageIntent = Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
pickImageIntent.type = "image/*" pickImageIntent.type = "image/*"
//pickImageIntent.putExtra("crop", "true") //pickImageIntent.putExtra("crop", "true")
pickImageIntent.putExtra("outputX", 1290) pickImageIntent.putExtra("outputX", 1290)
@ -147,7 +149,7 @@ class UserInfoActivity : AbsBaseActivity() {
private fun pickNewPhoto() { private fun pickNewPhoto() {
val pickImageIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) val pickImageIntent = Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
pickImageIntent.type = "image/*" pickImageIntent.type = "image/*"
pickImageIntent.putExtra("crop", "true") pickImageIntent.putExtra("crop", "true")
pickImageIntent.putExtra("outputX", 512) pickImageIntent.putExtra("outputX", 512)
@ -163,42 +165,30 @@ class UserInfoActivity : AbsBaseActivity() {
if (resultCode == Activity.RESULT_OK && data != null) { if (resultCode == Activity.RESULT_OK && data != null) {
when (requestCode) { when (requestCode) {
PICK_IMAGE_REQUEST -> { PICK_IMAGE_REQUEST -> {
val uri = data.data
try { try {
val bitmap = getResizedBitmap(getBitmap(contentResolver, uri), PROFILE_ICON_SIZE) data.data?.let {
val profileImagePath = saveToInternalStorage(bitmap, USER_PROFILE) val bitmap = getResizedBitmap(getBitmap(contentResolver, it), PROFILE_ICON_SIZE)
PreferenceUtil.getInstance().saveProfileImage(profileImagePath) val profileImagePath = saveToInternalStorage(bitmap, USER_PROFILE)
loadImageFromStorage(profileImagePath) PreferenceUtil.getInstance().saveProfileImage(profileImagePath)
loadImageFromStorage(profileImagePath)
}
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} }
} }
CROP_IMAGE_REQUEST -> {
val extras: Bundle = data.extras!!
val selectedBitmap: Bitmap = extras.getParcelable("data")
val profileImagePath = saveToInternalStorage(selectedBitmap, USER_PROFILE)
PreferenceUtil.getInstance().saveProfileImage(profileImagePath)
loadImageFromStorage(profileImagePath)
}
PICK_BANNER_REQUEST -> { PICK_BANNER_REQUEST -> {
val uri = data.data
try { try {
val bitmap = getBitmap(contentResolver, uri) data.data?.let {
val profileImagePath = saveToInternalStorage(bitmap, USER_BANNER) val bitmap = getBitmap(contentResolver, it)
PreferenceUtil.getInstance().setBannerImagePath(profileImagePath) val profileImagePath = saveToInternalStorage(bitmap, USER_BANNER)
loadBannerFromStorage(profileImagePath) PreferenceUtil.getInstance().setBannerImagePath(profileImagePath)
loadBannerFromStorage(profileImagePath)
}
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} }
} }
CROP_BANNER_REQUEST -> {
val selectedBitmap: Bitmap? = data.extras?.getParcelable("date")
val profileImagePath = selectedBitmap?.let { saveToInternalStorage(it, USER_BANNER) }
profileImagePath?.let {
PreferenceUtil.getInstance().saveProfileImage(it)
loadImageFromStorage(it)
}
}
} }
} }
} }
@ -213,8 +203,8 @@ class UserInfoActivity : AbsBaseActivity() {
val documentId = DocumentsContract.getDocumentId(aUri) val documentId = DocumentsContract.getDocumentId(aUri)
if ("com.android.providers.media.documents" == aUri.authority) { if ("com.android.providers.media.documents" == aUri.authority) {
val id = documentId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1] val id = documentId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]
val selection = MediaStore.Images.Media._ID + "=" + id val selection = Media._ID + "=" + id
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection) imagePath = getImagePath(Media.EXTERNAL_CONTENT_URI, selection)
} else if ("com.android.providers.downloads.documents" == aUri.authority) { } else if ("com.android.providers.downloads.documents" == aUri.authority) {
val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
java.lang.Long.valueOf(documentId)) java.lang.Long.valueOf(documentId))
@ -233,50 +223,13 @@ class UserInfoActivity : AbsBaseActivity() {
val cursor = App.context.contentResolver.query(aUri, null, aSelection, null, null) val cursor = App.context.contentResolver.query(aUri, null, aSelection, null, null)
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)) path = cursor.getString(cursor.getColumnIndex(Media.DATA))
} }
cursor.close() cursor.close()
} }
return path return path
} }
private fun performBannerCrop(picturePath: Uri?) {
val photoUri = FileProvider.getUriForFile(this, "$packageName.provider", File(getImagePathFromUri(picturePath)))
try {
val cropIntent = Intent("com.android.camera.action.CROP")
cropIntent.setDataAndType(photoUri, "image/*")
cropIntent.putExtra("crop", "true")
cropIntent.putExtra("aspectX", 1)
cropIntent.putExtra("aspectY", 1)
cropIntent.putExtra("return-data", true)
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(cropIntent, CROP_BANNER_REQUEST)
} catch (anfe: ActivityNotFoundException) {
val errorMessage = "your device doesn't support the crop action!"
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
}
}
private fun performCrop(imageUri: Uri) {
val photoUri = FileProvider.getUriForFile(this, "$packageName.provider", File(getImagePathFromUri(imageUri)))
try {
val cropIntent = Intent("com.android.camera.action.CROP")
cropIntent.setDataAndType(photoUri, "image/*")
cropIntent.putExtra("crop", "true")
cropIntent.putExtra("aspectX", 1)
cropIntent.putExtra("aspectY", 1)
cropIntent.putExtra("outputX", 280)
cropIntent.putExtra("outputY", 280)
cropIntent.putExtra("return-data", true)
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(cropIntent, CROP_IMAGE_REQUEST)
} catch (anfe: ActivityNotFoundException) {
val errorMessage = "your device doesn't support the crop action!"
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
}
}
private fun loadBannerFromStorage(profileImagePath: String) { private fun loadBannerFromStorage(profileImagePath: String) {
disposable.add(Compressor(this) disposable.add(Compressor(this)
.setQuality(100) .setQuality(100)
@ -323,9 +276,7 @@ class UserInfoActivity : AbsBaseActivity() {
companion object { companion object {
private const val PICK_IMAGE_REQUEST = 9002 private const val PICK_IMAGE_REQUEST = 9002
private const val CROP_IMAGE_REQUEST = 9003
private const val PICK_BANNER_REQUEST = 9004 private const val PICK_BANNER_REQUEST = 9004
private const val CROP_BANNER_REQUEST = 9005
private const val PROFILE_ICON_SIZE = 400 private const val PROFILE_ICON_SIZE = 400
} }
} }

View file

@ -98,12 +98,12 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/nameContainer" android:id="@+id/nameContainer"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
app:hintEnabled="true"> app:hintEnabled="true">
@ -119,12 +119,12 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/bioContainer" android:id="@+id/bioContainer"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
app:hintEnabled="true"> app:hintEnabled="true">
@ -135,7 +135,8 @@
android:background="@null" android:background="@null"
android:hint="@string/bio" android:hint="@string/bio"
android:inputType="textPersonName|textCapWords|text" android:inputType="textPersonName|textCapWords|text"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead" /> android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:visibility="gone" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
@ -147,8 +148,8 @@
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:layout_margin="16dp" android:layout_margin="16dp"
android:gravity="center" android:gravity="center"
app:iconGravity="textStart"
android:padding="16dp" android:padding="16dp"
android:text="@string/save" android:text="@string/save"
app:icon="@drawable/ic_save_white_24dp" /> app:icon="@drawable/ic_save_white_24dp"
app:iconGravity="textStart" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View file

@ -35,7 +35,9 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="12dp" android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
app:cardCornerRadius="12dp" app:cardCornerRadius="12dp"
app:cardUseCompatPadding="true"> app:cardUseCompatPadding="true">

View file

@ -26,7 +26,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="4dp" android:layout_marginStart="4dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="4dp"
android:layout_weight="1" android:layout_weight="1"
android:padding="10dp" android:padding="10dp"
android:text="@string/action_play_all" android:text="@string/action_play_all"
@ -41,7 +41,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="4dp" android:layout_marginStart="4dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="4dp"
android:layout_weight="1" android:layout_weight="1"
android:padding="10dp" android:padding="10dp"
android:text="@string/shuffle" android:text="@string/shuffle"