i'm using the below code to get an image from video that i want to upload it to server and the image will be my thumbnail image there ... it works correctly and shows the image in ImageView ... but some thing that is wrong is I can't upload it without saving on SD Card ... is there any way for me to upload Bitmap image directly to server without SD Card URI?
My Code:
private void chooseVideo() {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select a Video "), SELECT_VIDEO);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
System.out.println("SELECT_VIDEO");
selectedImageUri = data.getData();
textView.setText(selectedPath);
bt = createVideoThumbnail(this, selectedImageUri);
iv_image.setImageBitmap(bt);
}
}
}
public static Bitmap createVideoThumbnail(Context context, Uri uri) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(context, uri);
bitmap = retriever.getFrameAtTime(-1);
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
return bitmap;
}
try this
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
Then POST the image_str to the server, base64 decode it back to an image.
Related
I've been developed app to take a picture to be saved in gallery. I've searched online and the easiest way that I could practice was to use data.getExtras().get("data") . So below are the code to take picture from camera. Note that I'm using fragment in this class.
img22a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_01);
}
});
Get the captured image and convert it to string
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_01) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] byte_arr = output.toByteArray();
((SiteProgress) getActivity().getApplication()).seti22a1(Base64.encodeToString(byte_arr, Base64.DEFAULT));
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
img22a1.setBackgroundDrawable(ob);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
I save the string into global variable so another activity can access it.
In another activity, I have a button to save the image into folder/gallery by accessing the image string.
for(int i=0;i<=namefile.length;i++){
Bitmap[] bitmap = new Bitmap[namefile.length];
FileOutputStream outputStream = new FileOutputStream(String.valueOf(namefile[i]));
bitmap[i] = BitmapFactory.decodeByteArray(decodedString[i],0,decodedString[i].length);
bitmap[i].compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), namefile[i].getAbsolutePath(), namefile[i].getName(), namefile[i].getName());
}
It worked to save the image into folder/gallery....But the quality of image was very low...I could barely see text in the image.
They said data.getExtras().get("data") supposed to return thumbnail and not the actual image. What I want here is to get the full image and not the thumbnail one.
Any answer will be very appreciated.
From: Android - How to get the image using Intent data
if(data != null)
{
Uri selectedImageUri = data.getData();
filestring = selectedImageUri.getPath();
Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);
System.out.println(String.format("Bitmap(CAMERA_IMAGES_REQUEST): %s", thumbnail));
System.out.println(String.format("cap_image(CAMERA_IMAGES_REQUEST): %s", cap_image));
cap_image.setImageBitmap(thumbnail);
}
I have implemented image cropping in my app using android intent like below:
CropIntent = new Intent("com.android.camera.action.CROP");
I have received the cropped image and displayed on my UI but I want to perform further use cases like upload the cropped image uri. The problem is that no uri is returned in activity.
Here's my crop image intent snippet:
galleryFAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Gallery Btn Clicked");
selectFromGallery();
}
});
private void selectFromGallery() {
galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(Intent.createChooser(galleryIntent, "Choose From"), 2);
}
my code in activity result method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
InputStream inputStream;
if (requestCode == 2 && resultCode == RESULT_OK){
if (data != null){
cropImageUri = data.getData();
userImage.setImageURI(cropImageUri);
try {
originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), cropImageUri);
inputStream = getContentResolver().openInputStream(data.getData());
originalBitmap = BitmapFactory.decodeStream(inputStream);
Log.d(TAG, "Original bitmap byte count is:\t" + originalBitmap.getByteCount());
resizedBitmap = getResizedBitmap(originalBitmap, 200);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Log.d(TAG, "Resized bitmap byte count is:\t" + resizedBitmap.getByteCount());
try {
File file = saveBitmap("avatar_image");
upLoadFile(file);
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = stream.toByteArray();
/**
* For Shared Preferences
* **/
encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
imagePrefs = getSharedPreferences("ImagePrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = imagePrefs.edit();
editor.putString("image_user", encodedImage).commit();
upLoadBytes(bytes);
} catch (IOException e) {
e.printStackTrace();
}
cropImage();
}
} else if (requestCode == PERM_CODE){
if (data != null){
//I want to get the cropped image uri here but unable to. Have tried converting the bitmap rcvd here to uri but in activity, ntn is returned. kindly guide me
Bundle bundle = data.getExtras();
originalBitmap = bundle.getParcelable("data");
userImage.setImageBitmap(resizedBitmap);
}
}
}
my crop image function:
private void cropImage() {
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(cropImageUri, "image/*");
CropIntent.putExtra("crop", true);
CropIntent.putExtra("outputX", 200);
CropIntent.putExtra("outputY", 200);
CropIntent.putExtra("aspectX", 1);
CropIntent.putExtra("aspectY", 1);
CropIntent.putExtra("scale", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, PERM_CODE);
} catch (ActivityNotFoundException ex){
}
}
Can anyone say how to get the output uri? Thanks.
If the cropped image is showing in an ImageView, follow this link to save it to device storage, then you can upload the file.
while debugging the below code, i am getting the value of picUri as null and i can see as picUri=null data:"Intent" {act=inline-data (has extras)}" in trace. Why does picUri is not having the corresponding uri and have data extras?
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
// use standard intent to capture an image
Intent captureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
Toast.LENGTH_SHORT);
toast.show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE) {
// get the Uri for the captured image
picUri = data.getData();
}
}
}
Here you pass the camera Intent
private void INTENTCAMERA() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
And after that captured your image
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
Log.e("ResultcapturedImage-->",mImageCaptureUri);
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
I have received the data in Bundle in onactivityresult and stored it into a file and got the uri from that file as shown below.
Bundle extras = data.getExtras();
Bitmap var_Bitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
var_Bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
try {
OutputStream out;
String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
File createDir = new File(root+"macro"+File.separator);
createDir.mkdir();
File file = new File(root + "macro" + File.separator +"macro.jpg");
file.createNewFile();
out = new FileOutputStream(file);
out.write(bytes);
out.close();
picUri= Uri.fromFile(file);
} catch (IOException e) {
// e.printStackTrace();
}
I am trying to make an app to select an image from the gallery or google photos and then crop it to make it my app background. The problem I am facing is that When I try to crop the picture using google photos then it gets saved but the app background doesn't change and I don't recive any crashes or any errors but when I crop it using the gallery app everything seems to work perfectly.
Here is my code to select photos:
Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
case SELECT_PICTURE: {
if (resultCode == RESULT_OK && null != data) {
mHandler.post(new Runnable() {
#Override
public void run() {
Uri selectedImageUri = data.getData();
InputStream imageStream;
Bitmap selectedImage;
try {
cropCapturedImage(getImageUrlWithAuthority(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(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri), filePathColumn, null, null, null);
cursor.moveToFirst();
imageStream = getContentResolver().openInputStream(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
selectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
//SharePreference to store image
PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
cursor.close();
//set gallery image
setChatBackground();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
});
}
Here is my cropIntent code:
public void cropCapturedImage(Uri picUri) {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 9);
cropIntent.putExtra("aspectY", 14);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_PICTURE);
}
ase CROP_PICTURE: {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String yourRealPath = cursor.getString(columnIndex);
} else {
//boooo, cursor doesn't have rows ...
}
cursor.close();
String v= data.toString();
if (resultCode == RESULT_OK && null != data) {
mHandler.post(new Runnable() {
#Override
public void run() {
try {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thePic.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
//SharePreference to store image
PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
//set gallery image
setChatBackground();
} catch (NullPointerException e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
});
}
No, Android Does Not Have a Crop Intent
Calling method startActivity() on an Intent with an action of com.android.camera.action.CROP. They are doing this to crop an image.
This is a really bad idea.
Source here - CommonsBlog
Try to log your selectedImageUri first.
I suspect that the google photos returns a url to online file, while the gallery app return a local file path.
I try to select an image from gallery and saved in parse cloud server in android.
But I am unable to do.
I have tried the following code:
OnImageView Click event choose image:
imageDish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
} catch (Exception e) {
Toast.makeText(getActivity(),
"Select Image From Gallery", Toast.LENGTH_LONG)
.show();
// TODO: handle exception
}
}
});
OnActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageDish.setImageURI(selectedImageUri);
}
}
}
#SuppressWarnings("deprecation")
public String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Parse Code for save image:
InputStream imageStream = null;
try {
imageStream = getActivity().getContentResolver().openInputStream(
selectedImageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
ParseFile file = new ParseFile("FoodImage", image);
// Upload the image into Parse Cloud
file.saveInBackground();
Log.d("File======", "" + file);
try {
ParseObject Foodobject = new ParseObject("Food");
Foodobject.put("FoodName", Name);
Foodobject.put("ResId", ParseObject.createWithoutData("Restaurant",Res_id);
Foodobject.put("FoodCategory", ParseObject.createWithoutData("FoodCategory", _CategoryId));
Foodobject.put("FoodDesc", Des);
Foodobject.put("Price",priceNumber);
Foodobject.put("VegOnly", "Y");
Foodobject.put("IsRecommended", false);
Foodobject.put("FoodImage", file);
Foodobject.saveInBackground();
} catch (Exception ex) {
Log.e("Error", "" + ex);
}
Here is my log output:
File======: com.parse.ParseFile#39f19700
Replace your code with this
ParseFile file = new ParseFile("FoodImage.png", image);
In parse docs it is clearly mentioned that you give a name to the file that has a file extension. This lets Parse figure out the file type and handle it accordingly.
https://parse.com/docs/android/guide#files
i think your issue on image size, it might be your selected image from gallery is too large to save in parse.com
so try this code.
It is working for me .
ByteArrayOutputStream stream = null;
Bitmap bitmap = BitmapFactory.decodeFile(picturePath)
Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
stream = new ByteArrayOutputStream();
newbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
final ParseFile file = new ParseFile("FoodImage.png", image);
file.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e==null){
// Your Parse Code....
}
}