Reputation: 420
The images i want to share are stored in the drawable. I read the image through adapter as i made arraylist of images. I am trying to share image using file provider. If i want to share with maybe Gmail or WhatsApp etc, i will not see image. How do source the image from hashmap "iImage"
In MainActivity i made int for list of images in drawable
//Listview icons for song titles in position
icon = new int[]{R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4, R.drawable.flower5, R.drawable.flower6, R.drawable.flower7, R.drawable.flower8, R.drawable.flower9, R.drawable.flower10};
This is what i have in the Adapter that sends the image to DetailActivity
public void onBindViewHolder(@NonNull final MyHolder7 myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); //here is position
myHolder.mDesc.setText(models.get(i).getDesc());
myHolder.mImageView.setImageResource(models.get(i).getIcon());
myHolder.mSubImageView.setImageResource(models.get(i).getIcon2());// here we used imge resource
myHolder.setItemCLickListener(new ItemClickListener7() {
@Override
public void onItemClickListener(View v, int position) {
String gTitle = models.get(position).getTitle();
String gDesc = models.get(position).getDesc();
int imageId = models.get(position).getIcon();
//get our data with intent
Intent intent = new Intent(mContext, DetailActivity7.class);
intent.putExtra("actionBarTitle", models.get(position).getTitle());
intent.putExtra("brandNewDesc", models.get(position).getBrandNewDesc());
intent.putExtra("iImage", imageId);
mContext.startActivity(intent);
return;
}
});
}
This is what displayed the image in the DetailActivity.java i want to share using file provider
imageView = findViewById(R.id.ImageFoot1);
if (intent != null && !intent.getExtras().isEmpty()) {
imageResourceId = intent.getIntExtra("iImage", -1);
imageView.setImageResource(imageResourceId);
}
NOW THIS IS HOW I SHARED THAT FAILED ME
I have configured the Manifest this below
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider>
This is the xml/file_paths file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
And this is my share Code How do fix the exact hashmap that displayed the image through adapter for example this: imageResourceId = intent.getIntExtra("iImage", -1);
so that it will read the mage through the position through the adapter.
buttonShare = findViewById(R.id.btnImageShare);
buttonShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable drawable=imageView.getDrawable();
Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
try {
File file = new File(getApplicationContext().getExternalCacheDir(), File.separator +"iImage");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID +".provider", file);
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Upvotes: 1
Views: 1273
Reputation: 420
Yes! I have got it right!!! I'm very happy now. It is good to always apply effort so as to achieve aim.
If you are using Recylerview Adapter to pass image in full to DetailActivity, this solution will help you share the image received in DetailActivity so that you can share with social media apps.
This is how i received the image from Adapter with intent put extrass into DetailActivity
imageView = findViewById(R.id.ImageFoot1);
if (intent != null && !intent.getExtras().isEmpty()) {
imageResourceId = intent.getIntExtra("iImage", -1);
imageView.setImageResource(imageResourceId);
}
This is the Correct Image Share Code
Drawable drawable=imageView.getDrawable();
Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
try {
File file = new File(getApplicationContext().getExternalCacheDir(), File.separator +"iImage"+imageResourceId+".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID +".provider", file);
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Goodluck!
Upvotes: 1
Reputation: 177
It throws FileNotFoundException and the permission access is denied
2020-08-15 07:29:01.103 10745-10745/com.joseph.imagesshareW/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/share_image_1597472941101.png (Permission denied)
get user pemission for storage
In your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
ask runtime permission read through for how to request permission Here the code For Asking runtime permission
Or you can give manually permission for app in setting then try I hope it will work
Upvotes: 0