Sharing a png image in drawable folder in SKD 24
For SDK > 24
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
String path = getExternalCacheDir() + "/shareimage.jpg";
java.io.OutputStream out = null;
java.io.File file = new java.io.File(path);
try {
out = new java.io.FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path = file.getPath();
Uri bmpUri = FileProvider.getUriForFile(ShareApp.this , this.getApplicationContext().getPackageName() + ".provider", file);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, "Share with"));
How to add an image to the drawable folder in Android Studio?
For Android Studio 1.5:
- Right click on res -> new -> Image Asset
- On Asset type choose Action Bar and Tab Icons
- Choose the image path
- Give your image a name in Resource name
- Next->Finish
Update for Android Studio 2.2:
Right click on res -> new -> Image Asset
On Icon Type choose Action Bar and Tab Icons
On Asset type choose Image
On Path choose your image path
Next->Finish
The image will be saved in the /res/drawable folder.
Warning!
If you choose to use images other than icons in SVG or PNG be aware that it could turn grey if the image is not transparent. You can find an answer in comments for this problem but none of these are verified by me because I never encountered this problem. I suggest you to use icons from here: Material icons
Android : Share drawable resource with other apps
You can't pass a bitmap to an intent.
From what I see you want to share a drawable from your resources. So first you have to convert the drawable to a bitmap. And then You have to save the bitmap to the external memory as a file and then get a uri for that file using Uri.fromFile(new File(pathToTheSavedPicture)) and pass that uri to the intent like this.
shareDrawable(this, R.drawable.dish, "myfilename");
public void shareDrawable(Context context,int resourceId,String fileName) {
try {
//convert drawable resource to bitmap
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
//save bitmap to app cache folder
File outputFile = new File(context.getCacheDir(), fileName + ".png");
FileOutputStream outPutStream = new FileOutputStream(outputFile);
bitmap.compress(CompressFormat.PNG, 100, outPutStream);
outPutStream.flush();
outPutStream.close();
outputFile.setReadable(true, false);
//share file
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
shareIntent.setType("image/png");
context.startActivity(shareIntent);
}
catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
}
}
Share drawable image to other apps properly (sharing PNG to Whatsapp fails without FileProvider)
I solved this by properly implementing a FileProvider! This guide helped me so much, but I'll give a brief summary here.
Within the application
tag of your Manifest, start declaring your new FileProvider like so:
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>
Then, create a new directory called xml
(control-click on your res
in Android Studio and go to New > Directory). Then, create a new file within xml
called file_provider_paths
(control-click on the new xml
directory, and go to New > File, and name it file_provider_paths.xml
). Add this code to that new xml file:
<paths>
<cache-path name="cache" path="/" />
<files-path name="files" path="/" />
</paths>
Finally, use it in your MainActivity or wherever like so:
// create new Intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
// set flag to give temporary permission to external app to use your FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, imageFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
// Set type to only show apps that can open your PNG file
intent.setType("image/png");
// start activity!
startActivity(Intent.createChooser(intent, "send"));
To get that imageFile
from an image in my drawable
directory, I first converted it to a Bitmap, and then onto a File object, like so:
// create file from drawable image
Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.yourbeautifulimage);
File filesDir = getApplicationContext().getFilesDir();
File imageFile = new File(filesDir, "ABeautifulFilename.png");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.PNG, 100, os); // 100% quality
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
And you're done, every app can see your shared image now!
How do I add an image in the drawable folder to an ImageView in Android Studio 1.2?
try with this way of code there are three ways to set image on ImageView
imageView.setImageResource(R.drawable.play);
or
<ImageView
android:id="@+id/ivProfileBg"
android:src="@drawable/image_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
or
<ImageView
android:id="@+id/ivProfileBg"
android:background="@drawable/image_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I can't add images in my android studio project
a easy way to import images is the Batch Drawable import. For this you need to install a plugin. Check this source. Hope it helps
http://www.javahelps.com/2015/02/android-drawable-importer.html
How to access images from drawable folder in android studio according to my choice?
In your List Array processing class, define a Int array containing references to the images:
int imageRes[] = {R.drawable.image1, R.drawable.image2,...};
Now considering that only three indices are returned after processing:
ex:
indices[] ={1,5,6};
int imageArray[] - (Size can be three)
for(int i =0; i<3;i++){
imageArray[i] = imageRes[indices[i]];
}
Your ListView can contain one Image View. Create a custom adapter for the list view and pass imageArray as parameter.
Then you can set the Image for ImageView something like this:
imageView.setImageResource[imageArray[i]]
Related Topics
How to Change Bitmap Image Color in Android
Getting the Dimensions of the Soft Keyboard
Load an Image from Assets Folder
How to Fix: "You Need to Use a Theme.Appcompat Theme (Or Descendant) with This Activity"
How to Keep an Alertdialog Open After Button Onclick Is Fired
Starting Frame-By-Frame Animation
Autoincrement Versioncode with Gradle Extra Properties
Android Share Intent for a Bitmap - Is It Possible Not to Save It Prior Sharing
How to Generate Buildconfigfield with String Type
Android Studio 3.0 Execution Failed for Task: Unable to Merge Dex
Getting a Photo from a Contact
Convert Bitmap Array to Yuv (Ycbcr Nv21)
Android Resources$Notfoundexception: Resource Id #0X7F030027
File Res/Drawable/Abc_Ic_Ab_Back_Material.Xml from Drawable Resource Id #0X7F020016
How to Display Map in Android with Marker