I have been trying out the drawing api example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html and would like to save the drawing, I have looked through previous answers and tried the solutions given but cant get it to save.
I have replaced the code in the api for the emboss button with the code I am using to save, code below:
public boolean onOptionsItemSelected(MenuItem item) {
mPaint.setXfermode(null);
mPaint.setAlpha(0xFF);
switch (item.getItemId()) {
case COLOR_MENU_ID:
new ColorPickerDialog(this, this, mPaint.getColor()).show();
return true;
case EMBOSS_MENU_ID:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "screentest.jpg");
try {
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
开发者_运维百科 e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am trying to save the image directly to the sdcard, when I click the button to save, there are no errors but it doesnt save the file under the sd card, anyone got any ideas why this is not working?
Also I have added the permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Thanks in advance
Thanks
Edit: I think the issue is with permissions or the way it is creating the file, I have tried to create a folder as well and it doesn't either, do I need any other permissions?
To save the draw use the below code.
View content = findViewById(R.id.rlid);// get the rootview of drawing screen
content.setDrawingCacheEnabled(true); //This is taking screen shot of your screen
Use the below code to save it by name of your choice.I have used to alertdialog which prompts the user to enter the name of his choice to save the image to gallery.
AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this);
editalert.setTitle("Please Enter the name with which you want to Save");
final EditText input = new EditText(DrawingRoomScreen.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
editalert.setView(input);
editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
content.setDrawingCacheEnabled(true);
String name= input.getText().toString();
Bitmap bitmap = content.getDrawingCache();// your drawing View
//File f= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
//String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File("/sdcard/"+name+".png");
try
{
if(!file.exists())
{
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
}
catch (Exception e)
{
e.printStackTrace();
}finally
{
content.setDrawingCacheEnabled(false);// don't want previous draw to be cached. every time user opts o save the draw the new draw will be cached.
}
}
});
editalert.show();
My Screen Draw XML file.The first linear layout acts as background and the second one acts a drawing pad.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:orientation="vertical"
android:weightSum="1.0" >
<LinearLayout
android:id="@+id/view_drawing_pad"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
精彩评论