ImageView is blank after setting Uri (Android Studio) - android

So am actually implementing a feature where user selects image from gallery and then in this activity if the user clicks on crop button a layout from this library becomes visible https://github.com/ArthurHub/Android-Image-Cropper then the user can crop and then click another button to save it.
But the problem is after cropping the image when clicked on cropclose it the layout goes away and imageview shows the cropped image but it just shows a blank image.
Edit- I found the problem thanks to #Usama Altaf
So I added a Log logging the image uri and it said this
24333-24333/com.margsapp.messenger D/imageUri: null
So the problem is imageuri is null How to fix this?
public class SendImageActivity extends AppCompatActivity implements CropImageView.OnSetImageUriCompleteListener,
CropImageView.OnCropImageCompleteListener {
ImageView imageView;
ImageView cancel, cropopen,cropclose, edit_open,edit_close;
EditText text;
ImageButton btn_send;
CropImageView cropImageView;
PhotoEditorView photoEditorView;
Uri imageUri;
Intent intent;
RelativeLayout messagebox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_image);
intent = getIntent();
imageUri = Uri.parse(intent.getStringExtra("imageUri"));
btn_send = findViewById(R.id.btn_send);
imageView = findViewById(R.id.imageview);
imageView.setImageURI(imageUri);
cropopen = findViewById(R.id.cropopen);
cropclose = findViewById(R.id.cropclose);
cropImageView = findViewById(R.id.cropImageView);
cropopen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cropImageView.setImageUriAsync(imageUri);
cropImage();
}
});
cropclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageView.setVisibility(View.GONE);
cropImage();
cropImageView.getCroppedImageAsync();
}
});
}
private void cropImage() {
cropImageView.setOnSetImageUriCompleteListener(this);
cropImageView.setOnCropImageCompleteListener(this);
}
#Override
public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
if (error == null) {
Toast.makeText(getApplicationContext(), "Image load successful", Toast.LENGTH_SHORT).show();
} else {
Log.e("AIC", "Failed to load image by URI", error);
Toast.makeText(getApplicationContext(), "Image load failed: " + error.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
#Override
public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
handleCropResult(result);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
handleCropResult(result);
}
}
private void handleCropResult(CropImageView.CropResult result) {
if(result.getError() == null){
Toast.makeText(getApplicationContext(), "Image crop Success: ", Toast.LENGTH_LONG).show();
imageUri = result.getUri();
imageView.setImageURI(imageUri);
cropImageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
}
else {
Log.e("AIC", "Failed to crop image", result.getError());
Toast.makeText(
getApplicationContext(),
"Image crop failed: " + result.getError().getMessage(),
Toast.LENGTH_LONG)
.show();
}
}
}

Related

Crop Image with ArthurHub lib and pass result to other activity

Hello I am trying to crop an image from android camera. I camera is activted and the activity to crop the image is also activated.
But, I am not being able to pass the cropped image to another activity.
I searched, I cant find a solution. What iam doing wrong? Maybe, It is because the putExtras?
lib page:
https://github.com/ArthurHub/Android-Image-Cropper
my PhotoFragment.java
public class PhotoFragment extends Fragment{
private static final String TAG = "PhotoFragment";
//constants
private static final int PHOTO_FRAGMENT_NUM = 1;
private static final int GALLERY_FRAGMENT_NUM = 2;
private static final int CAMERA_REQUEST_CODE = 5;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo, container, false);
Log.d(TAG, "onCreateView: Started.");
Button btnLaunchCamera = (Button) view.findViewById(R.id.btnLaunchCamera);
btnLaunchCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: launching the camera.");
// Create intent to Open Image
Intent galleryIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(galleryIntent, CAMERA_REQUEST_CODE);
}
});
return view;
}
private boolean isRootTask(){
if(((ShareActivity)getActivity()).getTask() == 0){
return true;
}else{
return false;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("APP_DEBUG", String.valueOf(requestCode));
try {
// When an Image is picked
if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
CropImage.activity(selectedImage)
.setGuidelines(CropImageView.Guidelines.ON)
.setMinCropResultSize(800, 800)
.setMaxCropResultSize(1000, 1000)
.start(getContext(), this);
}
// when image is cropped
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Log.d("APP_DEBUG",result.toString());
if (resultCode == Activity.RESULT_OK) {
Uri resultUri = result.getUri();
Log.d("APP_DEBUG",resultUri.toString());
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), resultUri);
//pofilePic.setImageBitmap(bitmap);
Toast.makeText(getContext(), "sssss", Toast.LENGTH_SHORT).show();
if(isRootTask()) {
try {
Log.d(TAG, "onActivityResult: received new bitmap from camera" + bitmap);
Intent intent = new Intent(getContext(), NextActivity.class);
intent.putExtra(getString(R.string.selected_bitmap), bitmap);
startActivity(intent);
}catch (NullPointerException e){
Log.d(TAG, "onActivityResult: NullPointerException" + e.getMessage());
}
}else {
try {
Log.d(TAG, "onActivityResult: received new bitmap from camera" + bitmap);
Intent intent = new Intent(getContext(), AccountSettingsActivity.class);
intent.putExtra(getString(R.string.selected_bitmap), bitmap);
intent.putExtra(getString(R.string.return_to_fragment), getString(R.string.edit_profile_fragment));
startActivity(intent);
getActivity().finish();
}catch (NullPointerException e){
Log.d(TAG, "onActivityResult: NullPointerException" + e.getMessage());
}
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
else {
Toast.makeText(getActivity(), "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong"+e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
and My NextActivity.java is:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
mFirebaseMethods = new FirebaseMethods(NextActivity.this);
mcaption = (EditText) findViewById(R.id.caption);
setupFirebaseAuth();
ImageView backArrow = (ImageView) findViewById(R.id.ivBackArrow);
backArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: closing the activity.");
finish();
}
});
TextView share = (TextView) findViewById(R.id.tvShare);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to the final share screen.");
// upload the image to firebase
Toast.makeText(NextActivity.this,"Attempting to upload a new photo", Toast.LENGTH_SHORT).show();
String caption = mcaption.getText().toString();
// check if the intent came from gallery or from the camera
if(intent.hasExtra(getString(R.string.selected_image))){
imgUrl = intent.getStringExtra(getString(R.string.selected_image));
mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, imgUrl, null);
}
else if(intent.hasExtra(getString(R.string.selected_bitmap))){
bitmap = (Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap));
mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, null, bitmap);
}
}
});
setImage();
}
/**
* gets the image url from the incoming intent and displays the chosen image
*/
private void setImage(){
intent = getIntent();
ImageView image = (ImageView) findViewById(R.id.imageShare);
// check if the intent came from gallery or from the camera
if(intent.hasExtra(getString(R.string.selected_image))){
imgUrl = intent.getStringExtra(getString(R.string.selected_image));
Log.d(TAG, "setImage: got new image url: " + imgUrl);
// UniversalImageLoader handles null , oder wise we should check is image is null
UniversalImageLoader.setImage(imgUrl, image, null, mAppend);
}
else if(intent.hasExtra(getString(R.string.selected_bitmap))){
bitmap = (Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap));
Log.d(TAG, "setImage: got new Bitmap");
image.setImageBitmap(bitmap);
}
}
I expect to get the cropped image in the ImageView image of the NextActivity.java

Get image from image view and then crop (Android Image Cropper Library)

Hi I have been using Android Image Cropper library, though I have been able to crop images from getting them from the gallery.
But how would I start the image crop activity if I would just get the image from a Imageview and not selecting from the gallery or camera?
I have researched but you can only start the crop activity when using a image URI but maybe someone else has been able to do it.
This is my acitivty:
private Uri mCropImageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtExtracted = (TextView) findViewById(R.id.txtRetrieved);
btnStartCrop = (Button) findViewById(R.id.btnStartCrop);
imageView = (ImageView) findViewById(R.id.imgView);
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
imageView.setImageBitmap(bitmap);
btnStartCrop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
Toast.makeText(this, "Cropping successful, Sample: " + result.getSampleSize(), Toast.LENGTH_LONG).show();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setMultiTouchEnabled(true)
.start(this);
}
Thanks in advance for any help regarding this, and if anyone has opinions or better ways to go it will be greatly appreciated :D
Can you try with picaso.
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Picasso.with(this)
.load(result.getUri())
.into(((ImageView) findViewById(R.id.quick_start_cropped_image)));
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}

Image Cropper failed to start a new activity from the OnActivityResult

I am trying to pick an image from a GalleryActivity which contains a GridView of my local gallery images and start a CropActivity, for which I am using an external library called Android Image Cropper. After the crop I want to start a new ConfirmPhotoActivity to confirm the image and start uploading to server etc.
Now after I cropped the image and clicked on the crop button it always takes me back to the GalleryActivity, and the logcat doesn't throw any error messages...
Please see relevant codes as below:
GalleryActivity:
//set onClickListener to the grid image view.
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: selected an image " + imageURLs.get(position));
Uri imageUri = Uri.parse(mAppend + imageURLs.get(position));
startCrop(imageUri);
Log.d(TAG, "onItemClick: starting crop image activity");
}
});
}
private void startCrop(Uri imageUri){
CropImage.activity(imageUri)
.setAspectRatio(1,1)
.start(this);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK && data !=null) {
resultUri = result.getUri();
Log.d(TAG, "onActivityResult: imageUrl is" + resultUri.toString());
String imageUrl = resultUri.toString();
startActivity(new Intent(this, ConfirmPhotoActivity.class).putExtra("croppedImageUrl", imageUrl));
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
ConfirmPhotoActivity
public class ConfirmPhotoActivity extends AppCompatActivity {
SquareImageView squareImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_photo);
squareImageView = findViewById(R.id.confirm_photo_imageView);
Intent intent = getIntent();
String imageurl = intent.getStringExtra("croppedImageUrl");
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageurl, squareImageView, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
Much appreciated!
try this
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri))
{
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
else
{
// no permissions required or already grunted, can start crop image activity
startCropImageActivity(imageUri);
}
}
// handle result of CropImageActivity
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK)
{
profileIV.setImageURI(result.getUri());
/*Here you can start new activity by passing imageUri with intent*/
profileImageFilepath = result.getUri().getPath();
Log.d("cropImageUri", result.getUri().getPath());
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
Toast.makeText(this, "Failed to select photo", Toast.LENGTH_LONG).show();
}
}
}

video thumbnail of selected video while uploading on firebase storage

i am uploading video to firebase storage. i want to show the thumbnail of selected video on app screen
i have tried uploading image with the same code and it gives me the image preview of selected image, but it doesnot show any preview or thumbnail of selected video
moreover i want to know hoe to give the path of selected video as well..
main activity
public class MainActivity extends AppCompatActivity {
private static final int PICK_VIDEO_REQUEST = 3;
Button chooseImg, uploadImg;
ImageView imgView;
int PICK_IMAGE_REQUEST = 111;
Uri filePath;
ProgressDialog pd;
//creating reference to firebase storage
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://<<ur app url>>"); //change the url according to your firebase app
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chooseImg = (Button)findViewById(R.id.chooseImg);
uploadImg = (Button)findViewById(R.id.uploadImg);
imgView = (ImageView)findViewById(R.id.imgView);
pd = new ProgressDialog(this);
pd.setMessage("Uploading....");
chooseImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST);
}
});
uploadImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(filePath != null) {
pd.show();
StorageReference childRef = storageRef.child("vide.mp4");
//uploading the image
UploadTask uploadTask = childRef.putFile(filePath);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
pd.dismiss();
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
pd.dismiss();
Toast.makeText(MainActivity.this, "Upload Failed -> " + e, Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(MainActivity.this, "Select a video", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
//getting image from gallery
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting image to ImageView
imgView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

How can I store the imagePath of the image that I took using the camera activity into the database and retrieving the path?

how to store the imagePath of the image that I had captured using the camera activity to database. I also need to retrieve that imagePath and display on another activity?
Can someone help me please?
Update:
public class Image_secondPage extends Activity
{
public static final int CAMERA_RESULT = 1;
Button btn1;
ImageView imageView1;
private final String tag = getClass().getName();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image_secondpage);
imageView1 = (ImageView)findViewById(R.id.imageView1);
Button btnNext = (Button)findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Image_secondPage.this, ImagesPage.class);
startActivity(intent);
}
});
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))
{
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);
}
else
{
Toast.makeText(getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.i(tag, "Receive the camera result");
if(resultCode == RESULT_OK && requestCode == CAMERA_RESULT)
{
File out = new File(getFilesDir(), "newImage.jpg");
if(!out.exists())
{
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
imageView1.setImageBitmap(mBitmap);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
imageView1 = null;
}
I am supposed to get the imagePath that I have captured and show the image on the imageView that I had on the next class but I don't know how. Can someone help me?
try use this
bitmap = (Bitmap) data.getExtras().get("data");
this will give you bitmap image, then you can save it to database or anything you want.
let me know if this not solve your problem.

Categories

Resources