【问题标题】:Android fingerpaint api save安卓指纹api保存
【发布时间】:2013-03-18 21:58:56
【问题描述】:

我一直在尝试绘图 api 示例:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html 并想保存绘图,我查看了以前的答案并尝试了给出的解决方案,但无法保存。

我已经用我用来保存的代码替换了 api 中浮雕按钮的代码,代码如下:

 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();
        }

我正在尝试将图像直接保存到 sdcard,当我单击按钮保存时,没有错误但它没有将文件保存到 sd 卡下,有人知道为什么这不起作用吗?

我也添加了权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

提前致谢

谢谢

编辑:我认为问题在于权限或创建文件的方式,我也尝试创建一个文件夹,但它也没有,我需要任何其他权限吗?

【问题讨论】:

  • 对不起,我帮不上忙……
  • 感谢您的帮助 MByD,会继续努力
  • 我也遇到了同样的问题。但正如你所说,没有错误出现。但是当我点击保存按钮时,我得到了 ForceCLose 错误。请帮帮我。

标签: android


【解决方案1】:

要保存抽奖,请使用以下代码。

    View content = findViewById(R.id.rlid);// get the rootview of drawing screen
    content.setDrawingCacheEnabled(true);   //This is taking screen shot of your screen

使用下面的代码按您选择的名称保存它。我曾经使用 alertdialog 提示用户输入他选择的名称以将图像保存到图库。

    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 文件。第一个线性布局用作背景,第二个用作绘图板。

  <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>

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多