Open a specific folder by clicking menu item - java

I try to click on a menu item with the help of this method to open a specific folder that the app creates in internal memory when .txt file is recorded, but this is not possible even though the folder exists. Can you help me when I click the menu item to open MyFolder, please.
switch (item.getItemId()) {
case R.id.openFolder:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/MyFolder/");
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
break;
}

First of all, I would check if there is some file explorer on the system that would handle a request of opening directories this is an example.
Then your code should be modified to:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/MyFolder/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
also you may create the uri like this and I am not sure why this prefix is needed sometimes, maybe someone can explain to us:
Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath()+ "/MyFolder/");

Related

trying open folder with intent

i want to open a specific folder from menu drawer in android app but when i run the app it open recent files folder.
this is my code
if (id == R.id.action_downloaded) {
if (PermissionUtilities.isPermissionGranted(mActivity, PermissionUtilities.SD_READ_WRITE_PERMISSIONS, PermissionUtilities.REQUEST_READ_WRITE_STORAGE_DOWNLOAD)) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory() + "/" + myfolder + "/");
intent.setDataAndType(uri, "application/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), 1000);
}
Put as extra INITIAL_URI on your intent an uri the user choosed before with ACTION_GET_CONTENT.

How to create a share button for whatsapp, facebook or others app installed in my phone

I am developing an app, where I want to share the TEXT and IMAGE to other apps installed on my phone.
But when I click the share button Neither it shares TEXT nor IMAGE just empty it directs me to another app I chose for sharing.
below postDescription and postImage are my methods of model class, and I checked that am I getting values are not in a toast it gives there values properly.
Below is the code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
Uri uri = Uri.parse(postImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share With"));
So the above bunch of code was not working, then I found code through which I can share my post TEXT and IMAGE to WhatsApp only, I tried that but it shows file formate not supported inside WhatsApp.
below is the code for sharing to WhatsApp only:
Uri imgUri = Uri.parse(postImage);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
I want to know how to create a share button that works fine for WhatsApp and everything or only WhatsApp will do the work for me.
By removing the package from the intent, Android should display the list of apps that can handle the type of intent.
Uri imgUri = Uri.parse(postImage); //Provide the URI to the downloaded image, not an external URL
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("*/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
if (whatsappIntent.resolveActivity(packageManager) != null) {
startActivity(whatsappIntent)
}

Sharing image from android app

I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on WhatsApp.
The code is:
String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uriToImage= Uri.parse(imageUrl);
Log.d("Image", ""+uriToImage);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image:"));
}
});
What's happening is:
On WhatsApp I can share the image easily.
On Gmail, it says that the attachment could not be sent.
On Hangouts I get a toast which says Photo couldn't be found
On Facebook too, the post is not accompanied with the Image but I can post.
On Facebook Messenger, it crashes without opening.
The tutorial that I have followed for this is given here. The send binary content part of the tutorial is the one that I have implemented.
Another thing I tried was to set the image in an ImageView and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.
I also read and tried the answers to: Question 1 and Question 2 but to no avail.
Where am I going wrong?
try this,
try
{
File myFile = new File(share_image_path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Yes the #CommonsWare Answer is correct you missed the Schema in your Intent.putExtra() and that's why it is failing to read your image in other social media platform
Here's my solution
Uri fileUri = Uri.fromFile(new File(imagePath));
//No need to do mimeType work or ext
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image:"));
BTW it works on all the mentioned platform
Uri image_uri = Uri.parse("file://"+imagePath);
Another method of creating Uri and passing it to intent

Android gallery not updated after ACTION_IMAGE_CAPTURE

I'm using MediaStore.ACTION_IMAGE_CAPTURE to take a picture with MediaStore.EXTRA_OUTPUT to point to the Uri for the DCIM/Camera directory where all the other photo/video files are stored.
The photo file is successfully taken and I can see it using ES File Explorer and can view it inside my app. However it is not shown in the gallery when I use Intent.ACTION_PICK
Intent selectPictureIntent = new Intent(Intent.ACTION_PICK);
selectPictureIntent.setType("image/*");
I've read the other topics on updating the Gallery after the picture comes back using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
and also
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
What's going on here :(
why did you use LocalBroadcastManager? LocalBroadcastManager can only send broadcast data inside of your app. Try to use
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
sendBroadcast(intent);
Gallery always listens to URI change. After file event broadcast to Gallery, the data stored in Gallery will be updated.
it also can problem with KITKAT build version.. for sure you can use this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(out); \\out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

Homescreen shortcuts with icons

Am trying to create a homescreen shortcut programmatically on android. So far I've been able to add the shortcut itself with the following code:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.icon);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
But, the shortcut is installed using the default icon in my resources. However, I would like to fetch icons from my website and adding an icon to the shortcut.
First, I need to download this shortcut. Under the assumption that I have this done, and the icon is on the sdcard for example, I have been unable to set an drawable icon.
The following code:
try {
Uri contentURI = Uri.parse("http://mnt/sdcard/mytest/test.png");
ContentResolver cr = mContext.getContentResolver();
InputStream in;
in = cr.openInputStream(contentURI);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
Bitmap thumb = BitmapFactory.decodeStream(in,null,options);
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, thumb);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
The file definitely exist and I've verified that using adb shell... This piece of code shows the following error:
10-13 16:11:31.184: WARN/System.err(23273): java.io.FileNotFoundException: No content provider: /mnt/sdcard/mytest/test.png
What am I doing wrong?
Thanks
You are trying to get bitmap from local resources (by using content provider).
To download Bitmap from server you should follow this:
Why is this image bitmap not downloading in Android?
It seems like your application unable to access test.png. Make sure it exists. Maybe you can start with local storage rather than sd card.

Categories

Resources