I want to save a FrameLayout
as an image and save it in my phone.
The FrameLayout
is inside a relative layout.
the FrameLayout
has an ImageView
and TextView
.
public class ImageText extends AppCompatActivity {
private static final String TAG = " a ";
FrameLayout frameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_text);
final FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
TextView tv1=(TextView)findViewById(R.id.textView1);
Intent intent=getIntent();
tv1.setText(intent.getStringExtra("text"));
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isStoragePermissionGranted();
saveFrameLayout(frameLayout);
}
});
}
public static void saveFrameLayout(FrameLayout frameLayout) {
frameLayout.setDrawingCacheEnabled(true);
frameLayout.buildDrawingCache();
Bitmap cache = frameLayout.getDrawingCache();
frameLayout.setDrawingCacheEnabled(false);
try {
FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+ "file.png");
cache.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
frameLayout.destroyDrawingCache();
}
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
}
Layout:
<?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:id="@+id/activity_image_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.notesk1.ImageText">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/frame"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/a"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="text"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:id="@+id/button3"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
LogCat:
01-02 12:03:26.941 5166-10018/? W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.touchtype.swiftkey/files/iris_messages_cache
01-02 12:03:26.961 8083-8083/? W/System.err: java.io.FileNotFoundException: /storage/emulated/0file.png: open failed: EACCES (Permission denied)
01-02 12:03:26.971 8083-8083/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
01-02 12:03:26.971 8083-8083/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-02 12:03:26.971 8083-8083/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
01-02 12:03:26.971 8083-8083/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
01-02 12:03:26.971 8083-8083/? W/System.err: at com.example.user.notesk1.ImageText.saveFrameLayout(ImageText.java:58)
01-02 12:03:26.971 8083-8083/? W/System.err: at com.example.user.notesk1.ImageText$1.onClick(ImageText.java:44)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.view.View.performClick(View.java:5714)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.widget.TextView.performClick(TextView.java:10926)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.view.View$PerformClick.run(View.java:22589)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.os.Looper.loop(Looper.java:148)
01-02 12:03:26.971 8083-8083/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7325)
01-02 12:03:26.971 8083-8083/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-02 12:03:26.971 8083-8083/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
01-02 12:03:26.971 8083-8083/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-02 12:03:26.971 8083-8083/? W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-02 12:03:26.971 8083-8083/? W/System.err: at libcore.io.Posix.open(Native Method)
01-02 12:03:26.971 8083-8083/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-02 12:03:26.971 8083-8083/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
01-02 12:03:26.971 8083-8083/? W/System.err: ... 15 more
try the following :
change your
isStoragePermissionGranted
() methodcatch the permission result:
show dialog for permission:
hope this will solve your permission related problems...