代码未在Android Studio Emulator中写入SD

时间:2016-01-20 11:41:27

标签: android email bitmap

我正在尝试编写一个应用程序,它会给我一个截图并通过电子邮件发送给我(不是客户端) 我被困在第一部分。 为什么代码没有将png图像捕获到SD卡中? 有人可以请帮助。 之后我在哪里放置代码将电子邮件发送给自己? 我已将此添加到我的清单中:

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

这是我的.java文件:

  import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.widget.RelativeLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends Activity {
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                final RelativeLayout layout=(RelativeLayout)findViewById(R.id.RelativeLayout01);

                layout.post(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bm=takeScreenShot(layout);

                        try{
                            if(bm !=null){
                                savedScreenShot(bm);
                            }
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                });

            }
        });
    }
    //TAKE SCREENSHOT
    private Bitmap takeScreenShot(View v)
    {
        Bitmap screenShot=null;
        try
        {
            //GET WIDTH AND HEIGHT
            int width=v.getMeasuredWidth();
            int height=v.getMeasuredHeight();

            screenShot=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

            //DRAW CANVAS
            Canvas c=new Canvas(screenShot);
            v.draw(c);
        }catch(Exception e){
            e.printStackTrace();
        }
        return screenShot;
    }

    //SAVE TO EXTERNAL STORAGE
    private void savedScreenShot(Bitmap bm){
        ByteArrayOutputStream bao =null;
        File file=null;

        try{
            //COMPRESS AND WRITE OUTPUT STREAM
            bao=new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG,40,bao);

            //WRITE AS A FILE TO SD CARD
            file=new File(Environment.getExternalStorageDirectory()+File.separator+"Pictures/order.png");
            file.createNewFile();

            FileOutputStream fos=new FileOutputStream(file);
            fos.write(bao.toByteArray());
            fos.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

这是Logcat

    01-20 11:52:50.399 30175-30201/com.example.publi.almatakescreenshot W/EGL_emulation: eglSurfaceAttrib not implemented
01-20 11:52:50.399 30175-30201/com.example.publi.almatakescreenshot W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad75f220, error=EGL_SUCCESS
01-20 11:52:58.544 30175-30181/com.example.publi.almatakescreenshot W/art: Suspending all threads took: 21.093ms
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at java.io.File.createNewFile(File.java:939)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at com.example.publi.almatakescreenshot.MainActivity.savedScreenShot(MainActivity.java:86)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at com.example.publi.almatakescreenshot.MainActivity.access$100(MainActivity.java:19)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at com.example.publi.almatakescreenshot.MainActivity$1$1.run(MainActivity.java:42)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at android.os.Looper.loop(Looper.java:148)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-20 11:52:58.713 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-20 11:52:58.715 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at libcore.io.Posix.open(Native Method)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err:     at java.io.File.createNewFile(File.java:932)
01-20 11:52:58.716 30175-30175/com.example.publi.almatakescreenshot W/System.err:   ... 10 more

0 个答案:

没有答案