Open an image using URI is missing icons on top when displayed - java

I'm using the following code to display an image that it's stored on my sd card:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/pp.jpg"), "image/*");
startActivity(intent);
This works ok, but it displays something like this:
If I open the image directly from my phone, it shows like this:
How can I show the rest of the icons that you see on top when I open it from my app? I need at least to show the share button. Thanks

With this intent, you are telling the OS to find an app that can handle opening an image and you're doing this without checking whether there even is an app to handle it. A 3rd party app is thus handling the intent and opening the image taking it out of your control. You could try and find the right intent or extras to get the desired effect from the 3rd party app, but it is unlikely that you will be able to get it and furthermore there are thousands of devices in Android land and each one could have a different app handling your intent.
In order to ensure the functionality is as desired, you should open the image in your own app and create a menu with the options you desire as bhargav instructed.
Also, you should start your intent like the following to be a little safer with it if you are still going to have 3rd party apps open your images.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/pp.jpg"), "image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}

You have to create new Activity with an ImageView in center. And add all your menu icons in your menu.xml file of this activity as below.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu1"
android:icon="#drawable/icon_menu1"
android:title="menu1" />
<item android:id="#+id/menu2"
android:icon="#drawable/icon_menu2"
android:title="menu2" />
.......
</menu>

If the activity that is showing the image is nor your activity, you have no control over what, if any, options that activity gives the user. That activity was written by another developer, and that developer is free to do what she wants.
Moreover, there are thousands of Android device models. There will be hundreds of different image-viewing apps pre-installed on those device models, and there will thousands more available for download from places like the Play Store. The user will choose the app to view the image. What options, if any all those apps offer will be up to the thousands of developers who wrote those apps, not you.
If you want absolute control, write your own image-viewing activity.

Related

How to change the action bar color and open a file on click?

I'm working now on an app for Android and there are two things that I don't know how to do.
I want to change the colour of the action bar at the request of the user, so I want to change it dynamically. I see some answers that say that I need to use:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")));
But for some reason, it does not work for me.
I've added to my app option to download a file, but I do it in an unusual way so it does not create a notification when the download is complete. I want to give my user an easy way to get to the file. Can I create a button that opens the file when clicked?
Thanks!
1) For that I would recommend you having Example here Toolbar View and set it as your Actionbar. Just add this view at the top:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And then in Activity
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
After this you can change everything you want to the view myToolbar. Set background color etc. It will work. Using just Actionbar is old and depricated approach.
2) Yes you can, it is like opening new Activity with intent. Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.
Suppose If you have file type which categorized in document type you can use,
ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)
or If you going to handle some media file (Audio/Video)
you can use,
ACTION_VIEW
To identify MIME_TYPE of specific file you can use function
guessContentTypeFromName (String url)Link
Or getMimeTypeFromExtension(String extension)Link
Hope this helps :)
You can't change the color because you should call getSupportActionBar() and it's also for api >11;
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)‌​));
or
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")​));
About your second question, we need more info and your code where you download file. You totally can access files onClick but you need to know the file type and the directory where you store the files and target api to check runtime permissions for example
I suggest you ask another question with all your code and this info

Bluetooth discoverability request and screen orientation

Asking user the permission to turn device discoverable with the following snippet
final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent, BT_REQUEST);
causes the following system dialog to show up in portrait mode while my calling activity is in landscape, which is not a good user experience.
Many landscape only bluetooth apps I tried from the Play Store seem to suffer the same problem.
Is there any way to force the dialog to appear in landscape mode?
I tried to override system activity orientation from manifest, without success
<activity android:name="com.android.settings.bluetooth.RequestPermissionActivity"
android:screenOrientation="landscape" />

Display pdf image on click that is scrollable in another page

What I want to happen is that when a button is clicked, the user gets sent to another page and then a pdf file is openly displayed (the contents of it) and it is scrollable.
I have absolutely no clue how to go about this, which is why I am posting it in another question because I am aware that there is probably an answer that already explains how to do this, but I am so new to it I do not have any clue how to start!
The software is for an app (android studio), so I cannot use javascript to do it ( I do not think)
It is not a duplicate of that because I really have no clue how to solve the problem as I am too 'beginner-like' and also the fact that that article shows how to do it with internet and I cannot do it with internet for my application
I have done similar case in past. What I did is, I load a image by image from the pdf to android ImageView by passing the page number. I did this using Debenu PDF Library For Android
When User click your first Button Open separate activity and get the first page as a Image by passing page number to the pdf library inside the OnCreate method of Activity
If you need more help, feel free to ask..
There are already detailed answers on Stackoverflow: For instance:
How to open/display documents(.pdf, .doc) without external app?
In order to go to a new Page you use :
startActvity(new Intent(context, ActivityName.class));
The new Activity holds an WebView
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then you can use
WebView wv = (WebView)findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(doc);
Check this, Hope it will help -
1) Render a PDF file using Java on Android
2) https://github.com/jblough/Android-Pdf-Viewer-Library

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

hot to set a new title to my android appication in "share via" screen?

I have added my app to the images "share via" screen
(adding an intent filter in the manifest)
but I see all apps are organized alphabetically.
How can I change the title of my app so it will appear at the beginning?
I have seen dropbox changed theirs to "add to dropbox"
I want to change only the title for this specific perpuse. Not to change my app name for any use.
Try android:label on the ACTION_SEND <intent-filter>. Since this is static, you cannot guarantee that you will be first, but you can use "Add to" the way Dropbox did.

Categories

Resources