Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
InstiApp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
RAHUL SHARMA
InstiApp
Commits
d9b6a722
Commit
d9b6a722
authored
Jul 01, 2018
by
Sajal Narang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Image Upload bug
parent
a05de136
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
0 deletions
+59
-0
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/fragment/AddEventFragment.java
...n/ac/iitb/gymkhana/iitbapp/fragment/AddEventFragment.java
+59
-0
No files found.
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/fragment/AddEventFragment.java
View file @
d9b6a722
...
...
@@ -6,13 +6,17 @@ import android.app.ProgressDialog;
import
android.app.TimePickerDialog
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.database.Cursor
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.provider.MediaStore
;
import
android.support.v4.app.ActivityCompat
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v4.widget.ImageViewCompat
;
import
android.util.Base64
;
import
android.util.Log
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.ViewGroup
;
...
...
@@ -98,6 +102,17 @@ public class AddEventFragment extends BaseFragment {
// Required empty public constructor
}
public
static
String
convertImageToString
(
Bitmap
imageBitmap
)
{
ByteArrayOutputStream
stream
=
new
ByteArrayOutputStream
();
if
(
imageBitmap
!=
null
)
{
imageBitmap
.
compress
(
Bitmap
.
CompressFormat
.
JPEG
,
60
,
stream
);
byte
[]
byteArray
=
stream
.
toByteArray
();
return
Base64
.
encodeToString
(
byteArray
,
Base64
.
DEFAULT
);
}
else
{
return
null
;
}
}
@Override
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
...
...
@@ -288,8 +303,52 @@ public class AddEventFragment extends BaseFragment {
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
);
if
(
requestCode
==
RESULT_LOAD_IMAGE
&&
resultCode
==
RESULT_OK
&&
null
!=
data
)
{
Uri
selectedImage
=
data
.
getData
();
String
[]
filePathColumn
=
{
MediaStore
.
Images
.
Media
.
DATA
};
Cursor
cursor
=
getActivity
().
getContentResolver
().
query
(
selectedImage
,
filePathColumn
,
null
,
null
,
null
);
cursor
.
moveToFirst
();
int
columnIndex
=
cursor
.
getColumnIndex
(
filePathColumn
[
0
]);
String
picturePath
=
cursor
.
getString
(
columnIndex
);
cursor
.
close
();
ImageViewCompat
.
setImageTintList
(
eventPictureImageView
,
null
);
Picasso
.
with
(
getContext
()).
load
(
selectedImage
).
into
(
eventPictureImageView
);
base64Image
=
convertImageToString
(
getScaledBitmap
(
picturePath
,
800
,
800
));
Log
.
d
(
TAG
,
"onActivityResult: "
+
base64Image
);
}
}
private
Bitmap
getScaledBitmap
(
String
picturePath
,
int
width
,
int
height
)
{
BitmapFactory
.
Options
sizeOptions
=
new
BitmapFactory
.
Options
();
sizeOptions
.
inJustDecodeBounds
=
true
;
BitmapFactory
.
decodeFile
(
picturePath
,
sizeOptions
);
int
inSampleSize
=
calculateInSampleSize
(
sizeOptions
,
width
,
height
);
sizeOptions
.
inJustDecodeBounds
=
false
;
sizeOptions
.
inSampleSize
=
inSampleSize
;
return
BitmapFactory
.
decodeFile
(
picturePath
,
sizeOptions
);
}
private
int
calculateInSampleSize
(
BitmapFactory
.
Options
options
,
int
reqWidth
,
int
reqHeight
)
{
// Raw height and width of image
final
int
height
=
options
.
outHeight
;
final
int
width
=
options
.
outWidth
;
int
inSampleSize
=
1
;
if
(
height
>
reqHeight
||
width
>
reqWidth
)
{
// Calculate ratios of height and width to requested height and
// width
final
int
heightRatio
=
Math
.
round
((
float
)
height
/
(
float
)
reqHeight
);
final
int
widthRatio
=
Math
.
round
((
float
)
width
/
(
float
)
reqWidth
);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize
=
heightRatio
<
widthRatio
?
heightRatio
:
widthRatio
;
}
return
inSampleSize
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment