Blogger Text

My Aim to Provide you quality contents, Tips & Tricks, Software, Microsoft Office, Graphic Editing (Adobe PhotoShop, After Affects, Illustrator, inDesign) Corel Draw, Corel Video Studio, Cyberlink PowerDirector, Power ActionCinema, Tutorials about Blogging and VU Assignments, Quizes & GDB Solutions and Much More... at regular Basis
                                     ***    Kindly Subscribe our Official YouTube Channel "INFOPALACESS OFFICIAL-Tuts: in this channel we upload Programming (C,C++,C# JAVA, PHP), Web Development, Graphics Editing and Microsoft Office Step by Step Tutorials from bigginer to Advance Level. We also provide free online courses at our YouTube Channel. ***   Graded Assignments/Quizes and GDB will start in Next Week. Solution ideas of All assignments, Quizes and GDB will be available here. If you have any problem regarding this then you can contact us.

Share Pictures from ImageView In Android Studio without Saving into External Storage

Share Images from ImageView

ANDROID SHARING IMAGE USING SHARE INTENT

Today I am going to tell that how can we share images or files or other content without saving them to external memory. Because many users not do not grant permission for reading and writing of external storage due to some security concerns and this will save you from a lot of unnecessary code and permission in you application. Here we have to use a little hack which will save them into cache and then share it.

Let's Start! We can share our content by following the simple three steps.

  1. Add the provider tag in your AndroidManifest.xml file.
  2. Defile the filepaths.xml file in xml directory.
  3. Save the image in the cache and then share it
Step 1: First of All open Menifest file and add following code in your application tag in your Androidmanifest.xml file.
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.infopalacess.shareImage.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider>

Here com.infopalacess.shareImage is my application package name replace it with your own application name and append it with .fileprovider. For example com.youapplication.image.fileprovider.


Step 2: Now Create a new xml file in res folder and name as filepaths.xml and copy the following code and paste it there.
<!--xml version="1.0" encoding="utf-8"?--> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <cache-path name="shared_images" path="images/"/> </paths>
Step 3:

Now go to MainActivity.xml or whatever where you want to implement picture share method and add a Button and ImageView. Like Below;


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="31dp" android:src="@drawable/ic_icon"/> <Button android:id="@+id/btnshare" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/img" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:text="Share Image" android:onClick="shareImage"/> </RelativeLayout>

Step 4:

Now go Finally go to MainActivity.java and Create a Picture share method by copying following code mentioned.

Import ........ public class MainActivity extends Activity { Button btnshare; ImageView imageview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnshare = (Button) findViewById(R.id.btnshare); imageview = (ImageView) findViewById(R.id.img); } private void shareImage(Bitmap bitmap){ // save bitmap to cache directory try { File cachePath = new File(this.getCacheDir(), "imageview"); cachePath.mkdirs(); // don't forget to make the directory FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); stream.close(); } catch (IOException e) { e.printStackTrace(); } File imagePath = new File(this.getCacheDir(), "imageview"); File newFile = new File(imagePath, "image.png"); Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", newFile); if (contentUri != null) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri)); shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri); shareIntent.setType("image/png"); startActivity(Intent.createChooser(shareIntent, "Choose an app")); } } }

That's it we have done it. Now we can share image from imageview without saving into External Storage.

Post a Comment

0 Comments