FileProvider Save Camera Share UdayAndroid

package com.uday.resourcefileprovidermaster; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.app.ShareCompat; import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.DateFormat; import android.util.Log; import android.view.View; import android.webkit.MimeTypeMap; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.squareup.picasso.Picasso; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import it.federicoboschini.resourcefileprovider.ResourceFileProvider; import static it.federicoboschini.resourcefileprovider.ResourceFileProvider.FOLDER_RAW; public class MainActivity extends AppCompatActivity { File file,imgpath; Button btnShare,btnShare2; public final int REQUEST_CODE_TAKE_PICTURE =202; String imageFilePathl ; CharSequence fname; private static final String SHARED_FOLDER = "shared"; public File newFile; public ImageView imageView; private static final String SHARED_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider"; public File fp; static Uri photoURI; public File imagex,imagy,imagePathz,newFilezz; private static final int REQUEST_CAPTURE_IMAGE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnShare = (Button)findViewById(R.id.btn_share); btnShare2 = (Button)findViewById(R.id.button2); btnShare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //openCameraIntent(); openCameraIntentt(); } }); btnShare2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { shareImage(MainActivity.this); } }); } private void openCameraIntentt() { Intent pictureIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); if(pictureIntent.resolveActivity(getPackageManager()) != null){ //Create a file to store the image File photoFile = null; try { photoFile = createImageFile(); Log.d("22", "createImageFile2: "+photoFile); } catch (IOException ex) { // Error occurred while creating the File } if (photoFile != null) { photoURI = FileProvider.getUriForFile(this,"com.uday.resourcefileprovidermaster.fileprovider", photoFile); pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(pictureIntent, REQUEST_CAPTURE_IMAGE); } } } private File createImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); String imageFileName = "IMG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); // Log.d("22", "createImageFile: ext"+fileExt); File im2 = new File(storageDir,imageFileName); /* File image = File.createTempFile( imageFileName, *//* prefix *//* ".jpg", *//* suffix *//* storageDir *//* directory *//* ); */ imageFilePathl = im2.getAbsolutePath(); Log.d("22", "createImageFile: "+imageFilePathl); return im2; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { imageView = (ImageView)findViewById(R.id.imageView); /* if (requestCode == REQUEST_CAPTURE_IMAGE && resultCode == RESULT_OK) { if (data != null && data.getExtras() != null) { Bitmap imageBitmap = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(imageBitmap); } }*/ if (requestCode == REQUEST_CAPTURE_IMAGE) { //don't compare the data to null, it will always come as null because we are providing a file URI, so load with the imageFilePath we obtained before opening the cameraIntent // Glide.with(this).load(imageFilePath).into(mImageView); // If you are using Glide. Uri file = Uri.fromFile(new File(imageFilePathl)); Uri uri = Uri.parse(imageFilePathl); try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver() , uri); imageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } //Picasso.get().load(uri).placeholder(R.drawable.nav_head_img).into(imageView); } } public static void shareImage(Activity context) { // photoFile = createImageFile(); // Uri contentUri = FileProvider.getUriForFile(context, authority, newFile); Log.d("uri", "shareImage: "+photoURI); if (photoURI!=null) { Intent shareIntent = ShareCompat.IntentBuilder.from(context) .setType("image/png") .setStream(photoURI) .setSubject(context.getString(R.string.share_subject)) .setText(context.getString(R.string.share_message)) .setEmailTo(new String[]{"example@example.com"}) .getIntent(); shareIntent.setData(photoURI); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser( shareIntent, context.getString(R.string.share_chooser_title))); } } } <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.uday.resourcefileprovidermaster.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_provider_paths" /> </provider> <!--file_provider_paths--> <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <!-- <external-files-path name="my_images" path="my_images" /> --> <external-path name="my_images" path="Android/data/com.uday.resourcefileprovidermaster/files/Pictures" /> <!-- <external-files-path name="files" path="/" />--> <!-- <external-cache-path name="picturesext" path="EasyImage/" /> <cache-path name="picturesint" path="EasyImage/" />--> <!-- <external-path name="my_images" path="Android/data/com.uday.resourcefileprovidermaster/files/Pictures" />--> <!-- Share folder under device root directory. The base folder is new File("/") --> <!--<root-path name="root" path="" /> --> <!-- <!– Share folder under internal file folder. The base folder is context.getFilesDir() –> <files-path name="internal_files" path="" /> <!– Share folder under internal cache folder. The base folder is context.getCacheDir() –> <cache-path name="internal_cache" path="" /> <!– Share folder under public external storage folder.The base folder is Environment.getExternalStorageDirectory()–> <external-path name="external" path="Android/data/com.dev2qa.example/cache/" /> <!– Share folder under app specific external file folder.The base folder is context.getExternalFilesDirs()–> <external-files-path name="external_files" path="" /> <!– Share folder under app specific external cache folder.The base folder is context.getExternalCacheDirs()–> <external-cache-path name="external_cache" path="" />--> </paths><?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <!-- <external-files-path name="my_images" path="my_images" /> --> <external-path name="my_images" path="Android/data/com.uday.resourcefileprovidermaster/files/Pictures" /> <!-- <external-files-path name="files" path="/" />--> <!-- <external-cache-path name="picturesext" path="EasyImage/" /> <cache-path name="picturesint" path="EasyImage/" />--> <!-- <external-path name="my_images" path="Android/data/com.uday.resourcefileprovidermaster/files/Pictures" />--> <!-- Share folder under device root directory. The base folder is new File("/") --> <!--<root-path name="root" path="" /> --> <!-- <!– Share folder under internal file folder. The base folder is context.getFilesDir() –> <files-path name="internal_files" path="" /> <!– Share folder under internal cache folder. The base folder is context.getCacheDir() –> <cache-path name="internal_cache" path="" /> <!– Share folder under public external storage folder.The base folder is Environment.getExternalStorageDirectory()–> <external-path name="external" path="Android/data/com.dev2qa.example/cache/" /> <!– Share folder under app specific external file folder.The base folder is context.getExternalFilesDirs()–> <external-files-path name="external_files" path="" /> <!– Share folder under app specific external cache folder.The base folder is context.getExternalCacheDirs()–> <external-cache-path name="external_cache" path="" />--> </paths>
File

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.