I am trying to upload image in my app and i want to show some choices when my upload button is clicked. Unfortunately it gives me this error. I dont know what is wrong with the code. I got this from hereTrying open a specific folder in android using intent
thanks in advance!
uploadpic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
openFolder();
}
});
}
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/");
intent.setDataAndType(uri, "images");
startActivity(Intent.createChooser(intent, "Open folder"));
takephoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
profpic.setImageBitmap(imageBitmap);
}
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
You only want the user to select an image, am I right? Then maybe a general file picker isn't whats best suited for your needs.
A quick google search came up with this (also from SO). It seems like that solution is limited to pictures on an SD card.
If you don't want to click the link:
This is how you would start an intent in order to get an image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You'll then need to override onActivityResult(), but this is all described in the link.
Related
i know there is alot of diffrent tutorials online to teach u how to implement cropping, but im having trouble applying cropping function to my project. im building an OCR application, i used the onActivityresult to perform my OCR. i do not know how to add the cropping function into my code. Please help.
This is my button to press for gallery.
findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_GALLERY);
}
});
And this is my camera button.
findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String filename = System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
});
This is my onActivityresult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_GALLERY:
if (resultCode == RESULT_OK) {
inspect(data.getData());
}
break;
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
if (imageUri != null) {
inspect(imageUri);
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
because im passing my image into "inspect" class, how do i implement the cropping before passing them into inspect ?
You should not do much work in activity result, you can start a new thread to do complicated things and it's easy to do this using rxjava. Do this in you case:
Observable.just(imgUri)
.subscribeOn(Schedulers.io())
.map(uri -> MediaStore.Images.Media.getBitmap(getContentResolver(), uri))
.map(bitmap -> getCroppedImgAndReturnItsUri(bitmap))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(uri -> {inspect(uri);}, throwable -> {})
And don't forget to dispose this disposable on your activity stop.
I'm putting together a camera app using implicit intents for a college assignment, and I can't figure out what's going wrong with my Uri. I'm assigning a jpg file, then opening the camera using MediaStore. ACTION_IMAGE_CAPTURE as shown below. When I call selectImage(), I want the chosen viewing app to go straight to my recently taken photo. However, although the photo is saved and visible within the gallery, I'm getting an "Item not found" Toast, and I'm looking at an overview of all the image folders. Where am I going wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoFile = new File(Environment.getExternalStorageDirectory(),"cameraTest.jpg");
outputFile = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", photoFile);
Log.i("debugging",outputFile.getPath().toString());
}
public void capturePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(outputFile);
this.sendBroadcast(mediaScanIntent);
Log.i("debugging",outputFile.getPath().toString());
}
}
public void selectImage(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Log.i("debugging",outputFile.getPath().toString());
intent.setDataAndType(outputFile, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
I have created fragment with multiple image addition dialog. Each image can be added via camera or gallery. In both situations I can add only one image - after dismissing gallery/camera view application stops responding.
That's how I create camera and gallery pick intent:
//permissions request in other methods
private void pickImage() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_GALLERY);
}
private void openCamera() {
if (PermissionsHelper.checkStoragePermissions(ReportFragment.this)) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri();
fileFromCamera = fileUri.getPath();
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePicture, SELECT_PHOTO);
} else {
PermissionsHelper.requestStoragePermissions(this, CAMERA_STORAGE_PERMISSION_REQUEST);
}
}
Here is how result handled:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == SELECT_GALLERY) {
Uri selectedImageAddress = data.getData();
addPathsIfUnique(getRealPathFromURI(selectedImageAddress));
}
if (requestCode == SELECT_PHOTO) {
if (PermissionsHelper.checkStoragePermissions(this)) {
if(fileFromCamera != null) {
addPathsIfUnique(fileFromCamera);
}
} else {
PermissionsHelper.requestStoragePermissions(this, CAMERA_STORAGE_PERMISSION_REQUEST);
}
}
}
private void addPathsIfUnique(String path) {
if(path == null)
return;
for(String currentPath : photoPaths) {
if(currentPath.equals(path)) {
return;
}
}
photoPaths.add(path);
shopChanged(point);
}
In the shopChanged function I'm recreating ListView cells. I was also trying to completely remove ListView, replace it with single button in fragment, but result was same: after second time opened gallery or camera closed, application becomes not responding. Even OnResume breakpoint not fired. But after first opening all works good.
What can be reason for this behaviour?
Found a solution: this is new Google Play Services bug - issue fixed after downgrade to 8.4.0. Will search they bugtracker and submit bug, if it isn't submitted already
I want to add remove option in image chooser intent, I successfully added gallery option and camera option and those are working fine but I want to add remove option, if I choose remove option the image should remove from imageview.
Thanks in advance :)
My Code:
public void edit_profile_pic(View view) {
Uri uri = null;
Intent GalleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent CameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
Intent chooserIntent = Intent.createChooser(CameraIntent, "Choose");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {GalleryIntent, CameraIntent});
startActivityForResult(chooserIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
ImageView imageView = (ImageView) findViewById(R.id.profile_profile_picture);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Bitmap bitmap = null;
if(data.getData()!=null)
{
try
{
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
I was able to do this with an intent chooser by creating a new activity with the theme NoDisplay like this:
First create the activity which will be used to notify the main activity that the user wants to delete the image:
public class DeleteProfileImageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent resultIntent = new Intent();
resultIntent.putExtra("remove_image", true);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can specify the icon and label in the manifest. Make sure you also set the theme to NoDisplay to avoid it covering up the actual activity with the intent chooser.
<activity
android:name=".activity.profilesetup.DeleteProfileImageActivity"
android:label="#string/title_activity_delete_profile_image"
android:icon="#drawable/ic_delete_image_red"
android:theme="#android:style/Theme.NoDisplay"
/>
Then you can add it to the intent chooser. I check if the image has been set previously to avoid adding the option when there is no image.
if (imageSet) {
Intent removeImageIntent = new Intent(activity, DeleteProfileImageActivity.class);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {removeImageIntent} );
}
Then you can listen on activity result for intent data with the key specified in the DeleteProfileImageActivity, currently remove_image
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data.hasExtra("remove_image") && imageSet) {
// remove/reset the image here
}
}
You have to create a alert Dailog. There you can give option, and where you can add remove option. On remove option use this:
else if (options[item].equals("Remove")) {
ImageView.setImageDrawable(null);
dialog.dismiss();
}
For more help you can refer http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/
I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image