Tuesday, 1 September 2020
Saturday, 24 November 2018
Thursday, 24 May 2018
File sharing from external storage in android using FileProvider
Single/Multiple image/* files from External storage
sharing using FileProvider
Step 1:
Create an file_path provider in res>xml file as
Create an file_path provider in res>xml file as
file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/"/>
</paths>
Step 2:
Define the File provider in manifest.xml file as
<provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.whatsapp.statusdownloader.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Step 3:
Implement the Button action. For single file share
try {
//storage/emulated/0/Pictures/RAMP/IMG_19May2018_120740.jpg
String path = imagePaths.get(viewPager.getCurrentItem());
Uri contentUri = FileProvider.getUriForFile(ImageOpeningActivity.this, "com.whatsapp.statusdownloader.fileprovider", new File(path))
String shareable = "Status and File Downloader for whatsapp";
Log.e("path uri", newFile.getPath());
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setType(getContentResolver().getType(contentUri)); shareIntent.putExtra(Intent.EXTRA_TEXT, shareable);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
} catch (Exception e) {
e.printStackTrace();
}
For Multiple image share We should use the putParcelableArrayListExtra() for stream.
try {
final SparseBooleanArray selected = mAdapter
.getSelectedIds();
ArrayList<String> shareFiles = new ArrayList<>();
for (int i = 0; i < selected.size(); i++) {
Log.e("selected paths " + i, imageList.get(selected.keyAt(i)).getThumbnail());
shareFiles.add(imageList.get(selected.keyAt(i)).getThumbnail());
}
ArrayList<Uri> filesUri = new ArrayList<>();
for (int i = 0; i < selected.size(); i++) {
filesUri.add(FileProvider.getUriForFile(context,
"com.whatsapp.statusdownloader.fileprovider", new File(shareFiles.get(i))));
}
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setType(context.getContentResolver().getType(filesUri.get(0)));//DataAnd contentUri,
shareIntent.putExtra(Intent.EXTRA_TEXT, "Using statuses and file downloader for whatsapp");
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, filesUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
} catch (Exception e){
e.printStackTrace();
}
Tuesday, 17 April 2018
Tuesday, 27 June 2017
Name Live Wallpaper Example Android
Android Live Wallpaper:
This tutorial describes about how to implement the live wallpaper for android.
Steps to develop live wallpaper:
Step 1:
· Create an XML folder in resource(res) folder.
Right click on res->new->directory-> enter xml and click ok.
Right click on res->new->directory-> enter xml and click ok.
· Create an xml resource file for wallpaper as follows wallpaper.xml
write your description and icon
write your description and icon
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/wallpaper_service_description"
android:settingsActivity="blog.thiru.livewallpaper.WallpaperSettingsActivity"
android:thumbnail="@drawable/thumb" />
android:description="@string/wallpaper_service_description"
android:settingsActivity="blog.thiru.livewallpaper.WallpaperSettingsActivity"
android:thumbnail="@drawable/thumb" />
Here description attribute is the wallpaper description will be shown in list of wallpapers and thumbnail is the icon that displays in wallpaper list. If you want Settings Activity create a settings activity by extending AppCompatActivity. If you don’t want to change the settings of the live wallpapers, just remove the android:settingsActivity attribute.
Step 2:
Create Wallpaper settings activity.
There are 3 ways to create the wallpaper settings activity
· Create an activity extending the AppcompatActivity
· Create a class and extends PreferenceFragment.
· Create a class and extend to PreferenceActivity.
Here I used first approach.
Step 3: WallpaperService Class
Create a Service class which extends WallpaperService class, which is base class for all live wallpapers in the systems. Need to implement onCreateEngine() method and return an object o type android.service.wallpaper.WallpaperService.Engine. The Engine class has few Life cycle methods such as,
- onCreate();
- onSurfaceCreated();
- onVisibilityChanged();
- onOffsetChanged();
- onTouchEvent();
- onCommand();
public class Wallpaper extends WallpaperService { private static final String TAG = "Wallpaper";
int positionX, postionY;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
class WallpaperEngine extends Engine {
private final Handler handler = new Handler(); private final Runnable drawRunner = new Runnable() { @Override public void run() { draw(); } };WallpaperEngine() {nameImage = Utils.bitmap = textBitmap;bgImage = BitmapFactory.decodeResource(getResources(),R.drawable.background); postionX=-130; postionY=200; }public void onCreate(SurfaceHolder surfaceHolder){ super.onCreate(surfaceHolder); } @Overridepublic void onVisibilityChanged(boolean mVisible) { this.mVisible = mVisible; if (mVisible){ handler.post(drawRunner); } else handler.removeCallbacks(drawRunner); } @Override public void onSurfaceDestroyed(SurfaceHolder holder){ super.onSurfaceDestroyed(holder); this.mVisible = false; handler.removeCallbacks(drawRunner); } public void onOffsetsChanged(float xOffset,float yOffset,float xStep,float yStep,int xPixels,int yPixels){ draw(); } void draw() { final SurfaceHolder holder = getSurfaceHolder(); Canvas c = null; try{ c = holder.lockCanvas(); c.drawColor(Color.BLACK); if (c != null){ c.drawBitmap(bgImage, 0, 0, null); c.drawBitmap(nameImage, postionX,postionY, null);c.drawBitmap(nameImage, postionY,postionX, null);int width=c.getWidth(); if( postionX>width+100){ postionX=-130; } postionX = postionX+1;postionY = postionY+1;} } finally{ if (c != null) holder.unlockCanvasAndPost(c); } handler.removeCallbacks(drawRunner); if (mVisible){ handler.postDelayed(drawRunner, 10); // delay 10 mileseconds } }
}
Step 4:
Here is the Main Activity:
public class HomeActivity extends AppCompatActivity{
private AlertDialog.Builder alertDialog;
private View view;
private EditText fontText;
private TextView text1;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
text1 = (TextView) findViewById(R.id.txtToBitmap);
}
private void initDialog() {
alertDialog = new AlertDialog.Builder(this);
view = getLayoutInflater().inflate(R.layout.font_dialog, null);
fontText = (EditText) view.findViewById(R.id.fontText);
removeView();
alertDialog.setView(view);
alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
String font = fontText.getText().toString();
if(!font.equals("")) {
text1.setText(font);
text1.setTypeface(Typeface.createFromAsset(
text1.getContext().getAssets(),
"fonts/chunq_dipped.ttf"));
text1.post(new Runnable() {
@Override public void run() {
Bitmap textBitmap = getBitmapFromView(text1);
Utils.bitmap = textBitmap;
}
});
Intent intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(HomeActivity.this, MyWallpaperService.class));
startActivity(intent);
}
dialog.dismiss();
}
});
}
private void removeView() {
if (view.getParent() != null) {
((ViewGroup) view.getParent()).removeView(view);
}
}
public Bitmap getBitmapFromView(TextView view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
bgDrawable.draw(canvas);
view.draw(canvas);
return returnedBitmap;
}
}
activity_home.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/home_bg"
tools:context="com.ddrinfosystems.nameart.HomeActivity">
<TextView
android:id="@+id/txtToBitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#FFFF00"
android:visibility="invisible" android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="70dp" />
Step 5:</RelativeLayout>font_dialog.xml:<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"> <EditTextandroid:id="@+id/fontText"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"android:hint="Enter your name" android:layout_margin="16dp"></EditText> </LinearLayout>Utils class:public class Utils { static Bitmap bitmap;}
Setting up Manifest.xml
Live Wallpaper is a service, need to register the service in your manifest file. The Manifest file seems like..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blog.thiru.livewallpaper" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25" /> <uses-feature android:name="android.software.live_wallpaper" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/wallpaper_application_title" android:theme="@style/AppTheme" > <service android:name=".Wallpaper" android:label="@string/wallpaper_service_title" android:permission="android.permission.BIND_WALLPAPER" > <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" /> </service> <activity android:name=".WallpaperSettingsActivity" android:exported="true" android:label="@string/pref_screen_title" > </activity> <activity android:name=".WallpaperActivity" android:label="@string/wallpaper_application_title" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </activity> </application> </manifest>
Here it is just sample. You can customize the draw() method for your interest.
Subscribe to:
Posts (Atom)

