نحوه به اشتراک گذاری و share کردن عکس ها در اندروید چطور است؟
- نفیسه افقی 5 سال قبل سوال کرد
- آخرین ویرایش 5 سال قبل
- شما باید برای ارسال دیدگاه وارد شوید
روش به اشتراک گذاری متن در این پست توضیح داده شده. حالا می خواهیم عکس ها را share کنیم. برای اشتراک هر عکسی ، باید ابتدا آن را روی کش ذخیره کنیم و بعد آن را برای ارسال آماده کنیم. در نهایت کاربر می تواند با هر برنامه ای که خواست ، عکس ( و همراه آن متنی ) را share کند.
1- اول لازم است تا در فایل AndroidManifest.xml
، یک ContentProvider
به نام FileProvider
تعریف کنید:
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
*com.example.myapp
را با نام پکیج برنامه خود جایگزین کنید.
2- فایلی بنامres/xml/filepaths.xml
( در فولدر xml در قسمت res ) ایجاد کنید و کد زیر را در آن قرار دهید:
<?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>
3- عکس را به روش زیر در حافظه داخلی ذخیره کنید ( بصورت Bitmap
):
// save bitmap to cache directory
try {
File cachePath = new File(context.getCacheDir(), "images");
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.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4- و در نهایت عکس را share کنید:
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.myapp.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);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
*دقت کنید که در این روش ، ما به کارت حافظه گوشی یا sd کاری نداریم و از حافظه مختص برنامه استفاده می کنیم. بنابرین نیازی به اجازه دسترسی به نوشتن و خواندن نداریم و هر زمان که کاربر برنامه را حذف کند، عکس نیز همراه با آن حذف خواهد شد. در ضمن هر بار فایل عکس روی فایل قبلی نوشته می شود و یا به اصطلاح overwrite می شود.
اگر url یک عکس موجود در اینترنت را داشته باشید و بخواهید آن را همراه با یک متن share کنید، به شیوه زیر عمل کنید:
public void share_article(String link)
{
//Fetching and saving picture
Glide.with(getApplicationContext())
.asBitmap()
.load(link)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
Bitmap bitmap = resource;
File cachePath = new File(getCacheDir(), "images");
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.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
//Sharing text
String share_text = "با موستانگ یاد بگیر:" +"\n\n";
share_text += "\n\n برای مطالب بیشتر به وبسایت موستانگ سر بزن:"+"\nwww.mustang.ir";
share_text += "\n\n از اینجا هم میتونی اپلیکیشن موستانگ رو دانلود کنی:"+"\nwww.mustang.ir" ;
//Sharing Image
File imagePath = new File(getApplicationContext().getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.nafiseh.mustang.fileprovider", newFile);
//share
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.putExtra(android.content.Intent.EXTRA_TEXT,share_text);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
}
*به جای com.example.nafiseh.mustang.fileprovider
نام پکیج برنامه خودتان را قرار دهید.
- نفیسه افقی 5 سال قبل پاسخ داد
- آخرین ویرایش 5 سال قبل
- شما باید برای ارسال دیدگاه وارد شوید