I'm trying to access the image gallery and I have these code. Content Resolver turned red in the fragment. I've been trying tweaks, but still it isn't solved.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data!= null && data.getData() != null){
Uri filepath = data.getData();
bitmap = (Bitmap) MediaStore.Images.Media.getBitmap(getContentResolver, filepath);
}
}
Try these variations:
getActivity().getApplicationContext().getContentResolver()
or simply
getActivity().getContentResolver()
Related
I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)
myfile.exists() return TRUE, however I couldn't open the pdf:
File myfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator + fileString ); //fileString=sample.pdf
Log.i("here", fileString + "---"+ myfile.exists());
pdfView = findViewById(R.id.pdfViewer);
pdfView.fromFile(myfile)
.defaultPage(0)
.spacing(2)
.load();
I'm using this library for Pdf view in android.
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
If you wish, I can post all-working snippet code.
(using pdfView.fromUri() works fine but only once. My aim is to pick a .pdf from Intent.ACTION_OPEN_DOCUMENT once and save the uri of it for next usage.
Instead of pdfView.fromFile(), I used pdfView.fromUri(),
pdfView = findViewById(R.id.pdfViewer);
//pdfView.fromFile(myfile)
pdfView.fromUri(Uri.parse(getIntent_uri))
.defaultPage(0)
.spacing(2)
.load();
then after picking file, in onActivityResult() I used takePersistableUriPermission() for make the uri permanent accessible.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable #org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
special thanks to #CommonsWare
Hi I am new to java and android studio and have followed some tutorials to get images from the gallery and display it in a circular image view.
here is my code:
private void openImageFrom() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUri = data.getData();
avatar.setImageURI(imageUri);
}
}
The problem is that the circleImageView moves based on the image. I need it to stay in its fixed position. Does anyone know what the problem could be?
I am using this dependency in module:app:
implementation 'de.hdodenhof:circleimageview:3.0.1'
And this is my XML
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0px"
android:src="#drawable/profile_avatar"
app:civ_border_color="#color/DarkGreen"
app:civ_border_overlay="true"
app:civ_border_width="12dp" />
woops fixed my own issue nevermind.
changed XML:
android:layout_width="350dp"
android:layout_height="350dp"
While trying to get multiple selected images from gallery, there is a compatible issue as shown in screenshot when i use image array list. What could be the solution?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);
for (int i = 0; i < images.size(); i++) {
Uri uri = Uri.fromFile(new File(images.get(i).path));
// start play with image uri
}
As i said in comments You need to import the right Image .
import in.myinnos.awesomeimagepicker.models.Image;
instead of
import android.media.Image;
I want to be able to track image file names when a picture has been taken with the default Camera Glassware. This is so I can delete them when finished. I have the following code:
...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String imgPath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
mService.addToImageQueue(imgPath);
}
else if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_CANCELED) {
...
}
}
If I tap, the resultCode returns RESULT_OK. When I dismiss (swipe down), I get the resultCode RESULT_CANCELED. This is how I intended it to work, except it still generates the image file even if the resultCode is RESULT_CANCELED... I honestly feel like this might be a bug since I tried to use data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); and got a NullPointerException. Am I doing something wrong? Is there a way to get this file name even on RESULT_CANCELED?
You could create a temporary file first (look at the createImageFile() method in this tutorial). If successfully created, do two things:
Save the path of this file to a String.
Include this file's URI in the intent extra (putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))).
If resultCode is RESULT_CANCELED, you can now trace back to the path of the temporary file and call delete() on it.
Here is some sample code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.v("MainActivity", "Result successful.");
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_CANCELED) {
Log.v(TAG, "Result canceled. Uri of file is " + mCurrentPhotoPath);
File file = new File(mCurrentPhotoPath);
if (file.exists()) {
Log.v(TAG, "File exists.");
if(file.delete()) {
Log.v(TAG, "File was successfully deleted!");
} else {
Log.v(TAG, "File not successfully deleted.");
}
} else {
Log.v(TAG, "File does not exist!");
}
}
}
Note: For new File(mCurrentPhotoPath) to work, remove "file:" from the beginning of mCurrentPhotoPath.