Saturday, March 4, 2017

Create and change boot animation programmatically in android

                          please like & Subscribe our youtube channel

         https://www.youtube.com/playlist?list=PLQzJncZC8PUwCTcniu_akzp0N0oDx7l1n




Create Boot animation 





In given below example here will create bootanimation.zip file programmatically. In this example are first will create many jpeg files from a video file then will create part0 folder and desc.txt file again we will convert these part0 folders and desc.txt file in a bootanimation.zip file.


package com.create.bootaminmationcreater;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jcodec.api.FrameGrab.MediaInfo;
import org.jcodec.api.JCodecException;
import org.jcodec.api.android.FrameGrab;
import org.jcodec.common.FileChannelWrapper;
import org.jcodec.common.NIOUtils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.create.bootaminmationcreater.FileChooserDialog.OnFileSelectedListener;

public class Create extends Activity {
private TextView progress;
List<String> filesListInDir;
private volatile boolean flag;
InputStream in = null;
OutputStream out = null;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
progress = (TextView) findViewById(R.id.progress);
}

public void startDecode(View view) {
try {

File fileOrDirectory=new File("/sdcard/BootAnimation Creater");
DeleteRecursive(fileOrDirectory);
FileChooserDialog dialog = new FileChooserDialog(view.getContext());
dialog.addListener(new OnFileSelectedListener() {
public void onFileSelected(Dialog source, File folder, String name) {
}

public void onFileSelected(Dialog source, File file) {
source.hide();
new Decoder().execute(file);
}
});
dialog.show();
} catch (Exception e) {
}
}

void DeleteRecursive(File fileOrDirectory) {
  if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
 DeleteRecursive(child);
fileOrDirectory.delete();
}

public void stopProcess(View view) {
flag = true;
}

public void create(View view) {

try {
filesListInDir = new ArrayList<String>();

File dir = new File("/sdcard/BootAnimation Creater");
String zipDirName = "/sdcard/BootAnimation Creater/bootanimation.zip";

zipDirectory(dir, zipDirName);
} catch (Exception e1) {
}

Toast.makeText(getBaseContext(), "wait...........", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
public void run() {
try {
Toast.makeText(getBaseContext(), "Create Bootanimation",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}, 10000);

}

private void zipDirectory(File dir, String zipDirName) {
try {

populateFilesList(dir);

FileOutputStream fos = new FileOutputStream(zipDirName);
ZipOutputStream zos = new ZipOutputStream(fos);

byte[] buffer = new byte[1024];
int bytesRead;

CRC32 crc = new CRC32();

for (String filePath : filesListInDir) {

File file = new File(filePath);
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
crc.reset();
while ((bytesRead = fis.read(buffer)) != -1) {
crc.update(buffer, 0, bytesRead)
}
fis.close();

fis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(filePath.substring(dir.getAbsolutePath().length() + 1, filePath.length()));
entry.setMethod(ZipEntry.STORED);
entry.setCompressedSize(file.length());
entry.setSize(file.length());
entry.setCrc(crc.getValue());
zos.putNextEntry(entry);
while ((bytesRead = fis.read(buffer)) > 0) {
zos.write(buffer, 0, bytesRead);
}
zos.closeEntry();
fis.close();
}
zos.close();
fos.close();
} catch (IOException e) {
}
}
private void populateFilesList(File dir) throws IOException {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile())filesListInDir.add(file.getAbsolutePath());
else
populateFilesList(file);
}
}
private class Decoder extends AsyncTask<File, Integer, Integer> {
private static final String TAG = "DECODER";
protected Integer doInBackground(File... params) {
FileChannelWrapper ch = null;
try {
ch = NIOUtils.readableFileChannel(params[0]);
FrameGrab frameGrab = new FrameGrab(ch);
MediaInfo mi = frameGrab.getMediaInfo();
Bitmap frame = Bitmap.createBitmap(mi.getDim().getWidth(), mi.getDim().getHeight(), Bitmap.Config.ARGB_8888);
for (int i = 0; !flag; i++) {
frameGrab.getFrame(frame);
if (frame == null)
break;
OutputStream os = null;
try {
File folder = new File(
Environment.getExternalStorageDirectory()+ "/BootAnimation Creater");
boolean success = true;
if (!folder.exists())
success = folder.mkdir();
if (success) {
copydesc();
File folder1 = new File(
Environment.getExternalStorageDirectory()"/BootAnimation Creater/part0");
boolean success1 = true;
if (!folder1.exists())
success1 = folder1.mkdir();
if (success1) {
os = new BufferedOutputStream(new FileOutputStream(new File("/sdcard/BootAnimation Creater/part0",String.format("img%08d.jpg",i))));
frame.compress(CompressFormat.JPEG, 90, os);
}
}
} finally {
if (os != null)
os.close();
}
publishProgress(i);
if(i==50)
{
flag = true;
}
} catch (IOException e) {
} catch (JCodecException e) {
} finally {
NIOUtils.closeQuietly(ch);
}
return 0;
}
private void copydesc() {
in = getApplicationContext().getResources().getAssets().open("desc.txt");
out = new FileOutputStream("/sdcard/BootAnimation Creater/"+ "desc.txt");
copyFile(in, out);
}
private void copyFile(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
protected void onProgressUpdate(Integer... values) {
progress.setText(String.valueOf(values[0]));
      }

}
}

Change Boot animation programmatically


For change boot animation we will create ADB command programmatically. We can change boot animation two types because the bootanimation.zip file may exist in two location first one in /system/media/bootanimation.zip or in second /data/local/bootanimation.zip. 

First method


for change in first location /system/we will have to needed rooted device.
Runtime.getRuntime().exec(new String[] { "su", "-c","mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system/" });
Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /system/" });
Runtime.getRuntime()
.exec(new String[] {"su","-c","cat "+ f + " > /system/media/bootanimation.zip" });
Toast.makeText(getApplicationContext(),"You Suceesfully Change Bootanimation",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Wait till boot.....",Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
public void run() {
Runtime.getRuntime().exec(
new String[] { "su", "-c", "reboot" });
}
}, 20000);


second method


for change in second location /data/local/bootanimation.zip, it is not the necessarily rooted device.
Runtime.getRuntime().exec(new String[] { "su", "-c","mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system/" });
Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /system/" });
Runtime.getRuntime().exec(new String[] {"su","-c","cat "+ f + " > /data/local/bootanimation.zip" });
Toast.makeText(getApplicationContext(),"You Suceesfully Change Bootanimation",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Wait.....",Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
public void run() {
Runtime.getRuntime().exec(
new String[] { "su", "-c", "reboot" });
}, 20000);

2 comments:

  1. what is "f" in this line ?

    Runtime.getRuntime().exec(new String[] {"su","-c","cat "+ f + " > /data/local/bootanimation.zip" });

    ReplyDelete