[Q] Compiling problem with "AndroidManifest.xml"(Lollipop/SystemUI.apk) - Sony Cross-Device General

Hey, bros, i am trying to add "Delete" button to the ScreenShot Preview for Sony's Lollipop(5.0.2/242/Z Ultra).
while when i was testing, i found that the "Delete" button responded nothing.
then i decompiled the "SystemUI.apk" which was compiled just now and found the codes added to "AndroidManifest.xml" was missing.
Before(Part of "AndroidManifest.xml"):
Code:
<service android:exported="true" android:name="SystemUIService"/>
<service android:exported="false" android:name=".screenshot.TakeScreenshotService" android:process=":screenshot"/>
<receiver android:exported="true" android:name=".screenshot.DeleteScreenshot"/> #Added codes
<service android:exported="true" android:name=".LoadAverageService"/>
After(Part of "AndroidManifest.xml"):
Code:
<service android:exported="true" android:name="SystemUIService"/>
<service android:exported="false" android:name=".screenshot.TakeScreenshotService" android:process=":screenshot"/>
<service android:exported="true" android:name=".LoadAverageService"/>
Apktool 2.0.0 RC4:
Code:
I: Using Apktool 2.0.0-RC4
I: Smaling smali folder into classes.dex...
I: Building resources...
warning: string 'accessibility_quick_settings_airplane' has no default translati
on.
warning: string 'accessibility_quick_settings_bluetooth' has no default translat
ion.
warning: string 'battery_low_subtitle' has no default translation.
warning: string 'done_button' has no default translation.
warning: string 'jelly_bean_dream_name' has no default translation.
warning: string 'keyguard_accessibility_status' has no default translation.
warning: string 'kg_password_wrong_pin_code' has no default translation.
warning: string 'notifications_off_text' has no default translation.
warning: string 'notifications_off_title' has no default translation.
warning: string 'quick_settings_wifi_display_label' has no default translation.
warning: string 'quick_settings_wifi_display_no_connection_label' has no default
translation.
warning: string 'ssl_ca_cert_dialog_title' has no default translation.
warning: string 'ssl_ca_cert_info_message' has no default translation.
warning: string 'ssl_ca_cert_settings_button' has no default translation.
warning: string 'ssl_ca_cert_warning_message' has no default translation.
Warning: AndroidManifest.xml already defines versionCode (in http://schemas.andr
oid.com/apk/res/android); using existing value in manifest.
Warning: AndroidManifest.xml already defines versionName (in http://schemas.andr
oid.com/apk/res/android); using existing value in manifest.
I: Copy original files...
I: Copy AndroidManifest.xml...
I: Copy META-INF...
I: Building apk file...
And here is the Preview, do you have any idea? Thanks:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}

Code:
I: Copy original files...
I: Copy AndroidManifest.xml...
I: Copy META-INF...
You are using the "-c" option that means keep original AndroidManifest and signature, it's obvious that is unchanged
You must compile the systemui without that option and then sign the apk ( actually should work also without signing )

Hello!
I have a problem here and I need your help!
I decompiled an apk on my apktool(android version) because I have a bug on s6 gallery ported to Note 4. I found the lines and I made all changes I need, but when I tried to recompile the folder, it fails.
May I do something later change some smali lines?
Enviado do meu SM-N910C.

Please ask in the Q&A section for your device.
Cross device section is for development only
Thread closed

Related

[Q] Last question about database

I have currently developed 90% of my app, and i need help at the last part.
I want to thange the default database path to another user spezified path.
i get an error when i try this:
Code:
String myPath = "/data/data/com.android.providers.settings/databases/settings";
SQLiteDatabase myDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
The Debug monitor shows:
Code:
FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: unable to open database file
i hope someone here can help me
It can't open it because it's not there.
You need to create the DB in that "settings" subdirectory. Or you can just use adb shell and copy the DB to .../settings.
the database exists, found with root explorer
when i want to acces the database with:
SQLiteDatabase myDB = set.this.openOrCreateDatabase("/data/data/com.android.providers.settings/databases/settings.db", MODE_PRIVATE, null);
it says, unable to open file, that means that the database not exists, but there is one, please, thats the last part, i need help
ilendemli said:
Code:
String myPath = "/data/data/com.android.providers.settings/databases/settings";
SQLiteDatabase myDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
Click to expand...
Click to collapse
Didn't you forget the ".db" at the end of /data/data/com.android.providers.settings/databases/settings ?
tryed with db and without db, the same problem:
Code:
SQLiteDatabase myDB = set.this.openOrCreateDatabase("/data/data/com.android.providers.settings/databases/settings.db", MODE_PRIVATE, null);
What does the Debug monitor shows when you do openOrCreateDatabase ? (can't be "unable to open database", if it were it would create it)
I am catching the Exception and showing the error as toast message,
at the debugger there is this error:
Database: sqlite3_open_v2("/data/data/com.android.providers.settings/settings.db", &handle, 6, NULL) failed
According to this link :http://www.sqlite.org/c3ref/open.html, you should be able to execute sqlite3_errmsg to have a description of the error.
But I have to admit that I don't know if you can do that easily with the SDK :/ I don't really know how it handles errors in this case (cause "failed" isn't really speaking to me ^^)
Edit : Hey, I just spotted something ! You missed your shot in writing the path to settings.db !
You wrote : /data/data/com.android.providers.settings/settings.db
It is : /data/data/com.android.providers.settings/databases/settings.db
I tryed it.. annd.... the same error..
if i try:
Code:
SQLiteDatabase myDB = set.this.openOrCreateDatabase("/data/data/com.android.providers.settings/databases/settings.db", MODE_PRIVATE, null);
it should normally create a new database at the dir.
when i type /data/data/.file/settings.db
there will be created the database, but at com.android.providers.settings/databases/ or all other com.* folders, it haves no permission.. :/
Try to add WRITE_SECURE_SETTINGS permission to your manifest then. Perhaps it will unlock it
Code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
alot of permissions xD i will try it
€: nope, no permission.
Well, I think I've reached my limit ^^
I'll go with the "If you don't have and can't have the permission to touch it, then don't" and try to find an API that do the things you want.
BTW, can you explain what you want to do with this DB ?
i want to change "wifi_http_proxy" and "wifi_http_port"
Are you sure that these fields even exist ?
I can find http_proxy, but no wifi_http_proxy nor wifi_http_port :/
which android do you have? 2.1? or 2.2?
2.2, but I'm using the official SDK website as a reference (and Google, obviously ^^)
can you change proxy settings manually?
Virtual Device:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
My Phone:
Yes, I have the same screen as yours (2nd one).
hmm.. have you ever tryed to change the settings? try it to change, and then open the database /data/data/com.android.providers.settings/databases/settings.db -> system and then look for "wifi_http_proxy" and "wifi_http_port"
I'll try it once I get home. I'm at work now, and they locked the possibility to connect something with USB :/
I'll tell you what I'll see.

[Tutorial]How to remove title of the Power Menu

Tools needed :
My KIT for SMALI files (using options for each step )
Notepad++( Hexadecimal editor for *.xml )
_______________ Power Menu modded ______________
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Open my zip kit ( recomended in a Theming Folder for example with apkmanager and all files you would like to mod )
What you get :
Baksmali
Baksmali ( executable Jar File )
Command.exe ( Shell file )
Notepad text with the two commands for decompile and compile already made for you ( just copy and paste into command.exe )
Smali
Smali ( executable Jar File )
1.Pull out android.policy.jar ( located in /framework from the rom you are running.
2.Place it in the file where you unzip my Smali kit.
Example : C:\SMALI\android.policy.jar
3. You need to decompile android.policy.jar :
Open the cmd ready for you in the kit and paste
Code:
java -jar baksmali.jar -x android.policy.jar -o classout
(classout folder can be renamed to whatever you want, doesnt matter until you dont forget to change the name too in the second command)
4. Make the modifications you want for smali files, in this case : Open Global.Actions.smali with Notepad++
located in \\classout\com\android\internal\policy\impl\
5. CTRL+F to search the line "settitle"
Just over it, you should see a line looking like
Code:
const v2, 0x1040120
change to :
Code:
const v2, 0x1040017
6. Compile again android.policy.jar :
Open the cmd ready for you in the kit and paste
Code:
java -Xmx512M -jar smali.jar classout -o classes.dex
7. Copy and paste the file classes.dex into android.policy.jar _ you will overwrite the old "classes.dex"
8. Replace your modded android.policy.jar and flash it _ Enjoy your new Power Menu
Thanks to Rafalense
thanks a lot ..was looking for this one too ... you my friend are truly awesome
Thread closed at OP request.

Assistance with zRam configuration

I am the developer of the Bionx Catalsyt rom for the Droid Razr and I need some assistance with my toolkit I have developed for my rom (Its open sourced if youd like to contribute) I am relatively new to android app develoment so bare with me.
I need some assistance with zRAM configurations or a dev with some experience with it. I needed zRAM configurations for optimizing kernel usage on the Droid Razr to improve performance so I Ported the zRAM configurations from the CM10 Android Settings in my toolkit which led to me bringing over the Android System Properties (Reflected) and Utils Class for it to function correctly. 2 options are available to users. Configuration which gives users 4 options to choose from (disabled, 10%, 18%, 26%) and the option to enable zRAM or "Purge Memory". When I set a value, it seems not to "stick" or take affect but toggling Purge Settings does take affect. If i looked at a logcat, what values or what exactly would i be searching for to correct this feature or make sure it is taking affect or working at all?
and can any dev take a look at the code and see if I screwed anything up or miss writ something. (I didnt really change anything when it came from cm10, just wanting to port it to be useable on a stock based rom or AOSP)
Source
https://github.com/rgocal/app_packages_BionxDashboard.git
src/com/android/ZRAMconfig is the located class
src/android/os/ is location of imports needed
First, keep in mind you need a kernel with zRam support for those settings to work.
To set the zRam compression to the desired level (let's take 64mb as an example), you want to do something like this in a terminal :
Code:
su
busybox mkswap /dev/block/zram0
busybox swapon /dev/block/zram0
echo $((1024*1024*64)) > /sys/block/zram0/disksize [COLOR="Green"]// Replace 64 with the desired compression level[/COLOR]
So in java, this would translate to something like this :
A simple layout with an EditText for the user to input the desired zRam compression level & a Button to apply the value :
zram.xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
android:layout_marginTop="32dp"
android:ellipsize="end"
android:hint="Enter zRam Compression Level"
android:inputType="number" />
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:text="Apply"
android:textColor="#33b5e5" />
</LinearLayout>
The corresponding java Class :
Zram.java
Code:
package com.rgocal.zram.settings;
import java.io.DataOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Zram extends Activity implements OnClickListener {
private EditText input;
private Button apply;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zram);
input = (EditText) findViewById(R.id.userInput);
apply = (Button) findViewById(R.id.apply);
apply.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int value = Integer.parseInt(input.getText().toString());
// Arbitrary value, I don't know the maximum zRam compression (if
// there's one)
if (value > 200) {
Toast.makeText(
this,
"The value you entered is too high and may cause problems! Please reduce it",
Toast.LENGTH_SHORT).show();
} else {
try {
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("busybox mkswap /dev/block/zram0" + "\n");
os.writeBytes("busybox swapon /dev/block/zram0" + "\n");
os.writeBytes("echo $((1024*1024*" + value
+ ")) > /sys/block/zram0/disksize" + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
Log.v("zRam", "Compression level set to " + value);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Your AndroidManifest.xml :
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rgocal.zram.settings.Zram"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="zRam Settings"
android:theme="@style/AppTheme" >
<activity
android:name="com.rgocal.zram.settings.Zram"
android:label="zRam Settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The resulting apk : http://168.144.134.166/downloads/zRamSettings.apk
Screenshot :
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I uploaded the full code on my github to make it easier for you to grab it : https://github.com/Androguide/zRamSettings
Hope this gets you going, good luck
Androguide.fr said:
First, keep in mind you need a kernel with zRam support for those settings to work.
[*]The resulting apk : http://168.144.134.166/downloads/zRamSettings.apk
[*]Screenshot :
[/LIST]
I uploaded the full code on my github to make it easier for you to grab it : https://github.com/Androguide/zRamSettings
Hope this gets you going, good luck
Click to expand...
Click to collapse
Like a boss! Thanks!
Our kernel supports zRAM configurations with limitation but with Kexec support, we have more options so I needed some options for both ways
Update: Integrated this activity as a preference screen in my current zRAM configurations so now I have support both ways. Noticed your comment of how we dont know how to calculate a max/min RAM value so I'm looking for that value currently. Now all I need is to find a way to allow the activity to detect whether or not the device supports zRAM configurations and prevent the user from having this options if it isnt so it doesnt apply blank values.

Apk tool problem ... My fellow themers pleez help

ok using apk tool now...
decompiling went well
editing went well
but i have problem building it...
PHP:
D:\APKTool Tutorial - V2\APKTool\APKs>cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
D:\APKTool Tutorial - V2\APKTool\APKs>apktool if "D:\APKTool Tutorial - V2\APKTo
ol\APKs\SystemUI.apk"
I: Framework installed to: C:\Documents and Settings\Intel\apktool\framework\127
.apk
D:\APKTool Tutorial - V2\APKTool\APKs>apktool d "D:\APKTool Tutorial - V2\APKToo
l\APKs\SystemUI.apk"
I: Baksmaling...
I: Loading resource table...
I: Loaded.
I: Loading resource table from file: C:\Documents and Settings\Intel\apktool\fra
mework\1.apk
I: Loaded.
W: Could not decode attr value, using undecoded value instead: ns=android, name=
theme, value=0x010300a9
W: Could not decode attr value, using undecoded value instead: ns=android, name=
theme, value=0x010300a9
W: Could not decode attr value, using undecoded value instead: ns=android, name=
theme, value=0x010300a9
W: Could not decode attr value, using undecoded value instead: ns=android, name=
theme, value=0x010300a9
W: Could not decode attr value, using undecoded value instead: ns=android, name=
theme, value=0x010300a9
I: Decoding file-resources...
I: Decoding values*/* XMLs...
I: Done.
Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec:
0x010300a9
at brut.androlib.res.data.ResPackage.getResSpec(ResPackage.java:61)
at brut.androlib.res.data.ResTable.getResSpec(ResTable.java:55)
at brut.androlib.res.data.ResTable.getResSpec(ResTable.java:51)
at brut.androlib.res.data.value.ResReferenceValue.getReferent(ResReferen
ceValue.java:59)
at brut.androlib.res.data.value.ResReferenceValue.encodeAsResXml(ResRefe
renceValue.java:46)
at brut.androlib.res.data.value.ResScalarValue.encodeAsResXmlAttr(ResSca
larValue.java:43)
at brut.androlib.res.decoder.ResAttrDecoder.decode(ResAttrDecoder.java:4
0)
at brut.androlib.res.decoder.AXmlResourceParser.getAttributeValue(AXmlRe
sourceParser.java:315)
at org.xmlpull.v1.wrapper.classic.XmlPullParserDelegate.getAttributeValu
e(XmlPullParserDelegate.java:69)
at org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper.writeStartT
ag(StaticXmlSerializerWrapper.java:267)
at org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper.event(Stati
cXmlSerializerWrapper.java:211)
at brut.androlib.res.decoder.XmlPullStreamDecoder.decode(XmlPullStreamDe
coder.java:46)
at brut.androlib.res.decoder.ResStreamDecoderContainer.decode(ResStreamD
ecoderContainer.java:34)
at brut.androlib.res.decoder.ResFileDecoder.decode(ResFileDecoder.java:1
00)
at brut.androlib.res.AndrolibResources.decode(AndrolibResources.java:114
)
at brut.androlib.Androlib.decodeResourcesFull(Androlib.java:94)
at brut.androlib.ApkDecoder.decode(ApkDecoder.java:101)
at brut.apktool.Main.cmdDecode(Main.java:128)
at brut.apktool.Main.main(Main.java:65)
D:\APKTool Tutorial - V2\APKTool\APKs>apktool b "D:\APKTool Tutorial - V2\APKToo
l\APKs\SystemUI"
Exception in thread "main" brut.androlib.AndrolibException: brut.directory.PathN
otExist: apktool.yml
at brut.androlib.Androlib.readMetaFile(Androlib.java:143)
at brut.androlib.Androlib.build(Androlib.java:160)
at brut.androlib.Androlib.build(Androlib.java:155)
at brut.apktool.Main.cmdBuild(Main.java:182)
at brut.apktool.Main.main(Main.java:67)
Caused by: brut.directory.PathNotExist: apktool.yml
at brut.directory.AbstractDirectory.getFileInput(AbstractDirectory.java:
103)
at brut.androlib.Androlib.readMetaFile(Androlib.java:139)
... 4 more
D:\APKTool Tutorial - V2\APKTool\APKs>apktool b systemui
Exception in thread "main" brut.androlib.AndrolibException: brut.directory.PathN
otExist: apktool.yml
at brut.androlib.Androlib.readMetaFile(Androlib.java:143)
at brut.androlib.Androlib.build(Androlib.java:160)
at brut.androlib.Androlib.build(Androlib.java:155)
at brut.apktool.Main.cmdBuild(Main.java:182)
at brut.apktool.Main.main(Main.java:67)
Caused by: brut.directory.PathNotExist: apktool.yml
at brut.directory.AbstractDirectory.getFileInput(AbstractDirectory.java:
103)
at brut.androlib.Androlib.readMetaFile(Androlib.java:139)
... 4 more
D:\APKTool Tutorial - V2\APKTool\APKs>
oh wait there is no apktool.yml that shoud have been there in the decompiled apk... why is it??
that is causing the problem...
ok i know the problem now but what is causing it???
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
=========HOLO-UBUNTU========theme
problem not solve... problems recompinilg...
this is the new problem... and now there is apktool.yml
http://forum.xda-developers.com/show....php?t=2236091
Why don't you use APK Multitool instead?
iamareebjamal said:
Why don't you use APK Multitool instead?
Click to expand...
Click to collapse
i was using apk manager but i had to format system... dont want to download android sdk again..
added-
forget it i used vts as deathnotice suggested and that is just so awesome and works perfectly...
so problem solved...
u rock man deathnotice
mods pleez close this

[Tutorial] Create a Blur Effect on Android with RenderScript

Hello,
I create that thread to present you a tutorial aiming to learn you how to create a Blur Effect on Android with the RenderScript Library. You can discover the tutorial in video on Youtube too :
Create a Blur Effect on Android with RenderScript
In image processing, a Blur Effect, also known as Gaussian Blur, is the result of blurring an image by applying a Gaussian function. The Blur Effect is widely used in graphics software. In that tutorial, you're going to learn how to apply a Blur Effect on Android by using the RenderScript Library.
1. Configure your environment to use RenderScript
First step is to configure your development environment to enable RenderScript in your Android project. Note that your need the following Android SDK using these APIs:
Android SDK Tools revision 22.2 or higher
Android SDK Build-tools revision 18.1.0 or higher
You need to open your build.gradle and then add the following RenderScript properties :
Code:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}
}
2. Implement the Blur Effect with RenderScript
Note that we will use the RenderScript API in V8. This version is located in the android.support.v8.renderscript.* packages. So, be sure to import the correct classes in your code. We are going to create a BlurBuilder class with a static blur method taking the Bitmap to blur and returning the blurred Bitmap. We will use a blur radius constant to define which radius we want to apply on the original image.
Code for the BlurBuilder class should be like that :
Code:
package com.ssaurel.blureffect;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
/**
* Created by ssaurel on 29/09/2016.
*/
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.6f;
private static final float BLUR_RADIUS = 15f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
intrinsicBlur.setRadius(BLUR_RADIUS);
intrinsicBlur.setInput(tmpIn);
intrinsicBlur.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
Like you can see, we choose to apply a scale on the original bitmap but it's not mandatory. Then, we create a RenderScript instance. The RenderScript V8 API offers us a ScriptIntrinsicBlur class that implements a Blur Effect with RenderScript. We use it with the RenderScript instance in parameter. Then, we need to allocate memory for the input Bitmap and the ouput Bitmap. Last step is to configure the Blur Effect by applying the radius parameter.
Finally, we get the blurred image in result and we returned it.
3. Define images in your main layout
To see our Blur Effect in action, we need to create a layout with two images. The first will be used to show an initial image and the second to show the blurred image. We will use the following layout :
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ssaurel.blureffect.MainActivity">
<ImageView
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/originalImage"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:src="@drawable/logo_ssaurel"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultImage"
android:layout_below="@id/originalImage"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
4. Apply the Blur Effect
Now, it's time to apply the Blur Effect in the Main Activity of our application :
Code:
package com.ssaurel.blureffect;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView resultImage = (ImageView) findViewById(R.id.resultImage);
Bitmap resultBmp = BlurBuilder.blur(this, BitmapFactory.decodeResource(getResources(), R.drawable.logo_ssaurel));
resultImage.setImageBitmap(resultBmp);
}
}
We get our initial image as a Bitmap with the static method decodeResource of the BitmapFactory class, then we apply the Blur Effect on it. Finally, we set the blurred bitmap on the result image view.
5. Blur Effect Result
Now, you can enjoy the result of your Blur Effect :
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Don't hesitate to try to create this Blur Effect and give me your feedbacks.
Thanks.
Sylvain

Categories

Resources