How to apply Glide Library Transformation? [Android Studio] - android

So I have any Image in an ImageView loaded from my phone's gallery and I want to apply certain Glide transformation
The code should be like this
Glide.with(MainActivity.this)
.load(current image)
.bitmapTransform(new CropCircleTransformation(MainActivity.this))
.into(myimageview);
My problem is to know how to get the Bitmap, drawable, uri etc (whatever it works) from myimageview to ".load" that, so Glide knows that is the image it's going to transform ".into" myimageview.

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
ImageView mimageView;
String selectedImagePath;
Uri mCapturedImageURI;
int orientation;
ExifInterface exif;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mimageView = (ImageView) this.findViewById(R.id.image_from_camera);
Button button = (Button) this.findViewById(R.id.take_image_from_camera);
}
public void takeImageFromCamera(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
selectedImagePath = getRealPathFromURI_API19(this, data.getData());
Glide.with(MainActivity.this).load("file://" +selectedImagePath)
.bitmapTransform(new CropCircleTransformation(MainActivity.this))
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mimageView);
}
}
#SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
}

Related

Spring Boot REST Post Image Upload To Server In Android Using Multi Part Request

#RequestMapping(value="/upload/{id}", method=RequestMethod.POST)
public #ResponseBody BillUpload billUpload(#PathVariable Integer id,
#RequestParam("file") MultipartFile files) throws IOException{
if (!files.isEmpty()) {
FileUploadBean fileBean=new FileUploadBean();
fileBean.setFile(files.getBytes());
fileBean.setConsumerId(id);
fileBean.setFileName(files.getOriginalFilename());
String path= FileUploadProcess.createFile(fileBean);
BillUpload billUpload=new BillUpload();
billUpload.setUri(path);
return billuploadRepository.save(billUpload);
} else {
return new BillUpload();
}
}
This is RestController Which is a Multipart Request to Upload Image to Server.
I'm trying to Upload the selected Image from Gallery to Server Using Multi-part Request.
package com.example.takeimage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity implements View.OnClickListener {
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
Button btnSelect;
ImageView ivImage;
Button btnClick;
static String ConsumerId = " ";
String path="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
btnSelect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectImage();
}
});
ivImage = (ImageView) findViewById(R.id.ivImage);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
private void selectImage() {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
#SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaColumns.DATA};
Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
ivImage.setImageBitmap(bm);
}
#Override
public void onClick(View v) {
}
}
}
My MainActivity when the user selected any Image (from Gallery or Camera).
The selected Image must Uploaded to Server when The Upload Button is pressed.
As I added above the sending Request will be Multi Part Request (Post Method).
Can anyone Help me how to do this Thankful to them........
If you have any code share with me.

I want to store image in sqlite database that is taken from gallery and camera android

I have an application in which user add image by clicking on Imageview and setting image and by taking new picture from camera. I want to store that set image into sqlite database.
**AddkeyEventdetail.java**
package com.example.kidsfinal;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
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.DialogFragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.slidingmenuexample.R;
public class AddChildHoodEventActivity extends SmartActivity {
ImageView ivChildPics;
EditText edtEventDate, edtEventDetails;
Button btnEventDetailSubmit, btnopenCamera;
Spinner spinnerSelectChild;
Bitmap bmp;
Calendar calendar = Calendar.getInstance();
int mDay = calendar.get(Calendar.DAY_OF_MONTH), mMonth = calendar
.get(Calendar.MONTH), mYear = calendar.get(Calendar.YEAR) - 1;
#Override
public void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.addchildhoodevents);
createDrawer();
initComponent();
prepareView();
setOnListener();
}
private void initComponent() {
// TODO Auto-generated method stub
ivChildPics = (ImageView) findViewById(R.id.kids_addkeyevent_imageview_childpics);
edtEventDate = (EditText) findViewById(R.id.kids_addkeyevent_edt_addkeyevent);
edtEventDetails = (EditText) findViewById(R.id.kids_addkeyevent_edt_eventdetails);
btnEventDetailSubmit = (Button) findViewById(R.id.kids_addkeyevent_btn_submit);
btnopenCamera = (Button) findViewById(R.id.kids_addkeyevent_btn_opencamera);
}
private void prepareView() {
}
private void setOnListener() {
ivChildPics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
private void openGallery() {
Intent opengallery = new Intent(Intent.ACTION_GET_CONTENT);
opengallery.setType("Image/");
startActivityForResult(opengallery, 1);
}
});
btnopenCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
OpenCamera();
}
private void OpenCamera() {
Intent openCam = new Intent(
"android.media.action.IMAGE_CAPTURE");
startActivityForResult(openCam, 0);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
bmp = (Bitmap) data.getExtras().get("data");
ivChildPics.setImageBitmap(bmp); /*
* this is image view where you
* want to set image
*/
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
else {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if (bmp != null && !bmp.isRecycled()) {
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
ivChildPics.setBackgroundResource(0);
ivChildPics.setImageBitmap(bmp);
}
}
}
database.java
public void insertAddKeyEvent(byte[] childpic,String eventDate,String eventDetails)
{
ContentValues addkeyValues=new ContentValues();
addkeyValues.put("CHILDPICS",childpic);
addkeyValues.put("EVENTDATE", eventDate);
addkeyValues.put("EVENTDETAILS", eventDetails);
parentmaster.insert("ADDKEYEVENT",null,addkeyValues);
}
Convert your Bitmap into a byte[] by the following way
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
and add the byte[] into a BLOB field in your database.

saving picture into drawable file or my data base android

i have this code .. which work perfectly when i click the button i will choose a picture from my device gallery and then view it on imageview
what i want to do is can i save the slected image into drawable file o my database ?
package com.example.testpic;
import android.app.Activity;
import android.content.Intent;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity
{
Button btnGal;
ImageView ivGalImg;
Bitmap bmp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGal = (Button)findViewById(R.id.btnGallary);
ivGalImg = (ImageView)findViewById(R.id.ivImage);
btnGal.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
openGallery();
}
});
}
private void openGallery()
{
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
Uri selectedImage = intent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if(bmp != null && !bmp.isRecycled())
{
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
ivGalImg.setBackg`enter code here`roundResource(0);
ivGalImg.setImageBitmap(bmp);
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
}
}
you can not save images to your drawable folder after your apk file has been generated
you can save the path of image to your db
You can copy the image and past it to storage of your mobile programmatically, google for it

Print image location and put the image in image view

I am writing a android code to get the image location and after getting that put the physical image into a image view. SO far I have written code to open a popup window which allows user to select a file.
final int FILE_SELECT_CODE = 0;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent,
"Select a File to Upload"), FILE_SELECT_CODE);
} catch (Exception ex) {
Log.d("File upload", "error" + ex.toString());
}
Log.d("File Location", ":" + intent.getData().getPath());
I Tried this to get the location but it didnt show anything.
Now how do I get the file location and put it in a image view??
See this code for setting image in imageview.
package com.android.imagegalleray;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
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 = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}

How to open an image file only using Built in gallery android?

Hi I want to Open a image only using built in Gallery in android. Now I'm using the following code, If i click the button it shows the menu which contains the third party tools installed to open the image. I want only built-in gallery, Is there any option to hide the other Third party tools are I can open directly with Gallery without showing the menu.
package com.example.gallery;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BrowsePicture extends Activity {
private static final int SELECT_PICTURE = 1;
Button bt;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_picture);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Try to use
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
That's because on click of button you are passing intent in which you are setting its type intent.setType("image/*"); and intent.setAction(Intent.ACTION_GET_CONTENT); so it will show list of app installed on device.
If you want to open only gallery direct then you should pass intent for gallery only.

Categories

Resources