Step :
1. Convert your view in Bitmap like Liner to Bitmap , Frame to Bitmap , Image to Bitmap
2. Paste createDiarectoryandSaveImage() Method in your code.
3. Paste Share() following method;
/* Edit Menifests file Following code */
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication10">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/Theme.MyApplication10">
<activity
android:name=".ExternalDb.Save_Image"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExternalDb.ExActivity"
android:exported="true">
</activity>
<activity
android:name=".MainActivity"
android:exported="true"></activity>
</application>
</manifest>
createDiarectoryandSaveImage() :-
private void createDirectoryAndSaveFile(Bitmap imageToSave) {
ts = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString();
File direct = new File(Environment.getExternalStorageDirectory() + "/10AM");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/10AM/");
wallpaperDirectory.mkdirs();
}
file = new File("/sdcard/10AM/", ts+".png");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Share() : -
private void shareImage(Uri imagePath) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "http://codepath.com");
startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
}
Main Example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".ExternalDb.Save_Image">
<ImageView
android:src="@drawable/ruby"
android:id="@+id/img_view"
android:layout_width="200dp"
android:layout_height="200dp" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
MainActivity.java
package com.example.myapplication10.ExternalDb;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.myapplication10.R;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Save_Image extends AppCompatActivity {
private String ts;
private ImageView img_view;
Button save;
private Button share;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_image);
img_view=findViewById(R.id.img_view);
save=findViewById(R.id.save);
share=findViewById(R.id.share);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitamp = ((BitmapDrawable)img_view.getDrawable()).getBitmap();
createDirectoryAndSaveFile(bitamp);
}
});
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitamp = ((BitmapDrawable)img_view.getDrawable()).getBitmap();
createDirectoryAndSaveFile(bitamp);
shareImage(Uri.parse(file.getPath()));
}
});
}
private void createDirectoryAndSaveFile(Bitmap imageToSave) {
ts = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString();
File direct = new File(Environment.getExternalStorageDirectory() + "/10AM");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/10AM/");
wallpaperDirectory.mkdirs();
}
file = new File("/sdcard/10AM/", ts+".png");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void shareImage(Uri imagePath) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "http://codepath.com");
startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
}
}
0 Comments