创建文件路径但照片不会保存我一直得到捕获

时间:2017-11-25 21:08:47

标签: java android

 shareButton = (Button)findViewById(R.id.share_btn);

    ScreenShotHold = (ImageView)findViewById(R.id.imageView);

    bytearrayoutputstream = new ByteArrayOutputStream();

    shareButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View OnclickView) {

            view = OnclickView.getRootView();

            view.setDrawingCacheEnabled(true);

            bitmap = view.getDrawingCache();

            ScreenShotHold.setImageBitmap(bitmap);


            String foldername = "WhatsThis";
            mkFolder(foldername);


            try
            {
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG,8000,out);
                out.flush();
                out.close();
                Log.d("WhatsThisApp","Screenshot Saved!");

            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d("WhatsThisApp","Screenshot not Saved!");
            }

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri screenshotUri = Uri.parse("android.resource://" + getPackageName()
                    + "/drawable/" + "ic_launcher");
            sharingIntent.setAction(Intent.ACTION_SEND);

            sharingIntent.setType("image/jpeg");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
            sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));
        }
    });

}


public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
public int mkFolder(String folderName){ // make a folder under Environment.DIRECTORY_DCIM
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)){
        Log.d("myAppName", "Error: external storage is unavailable");
        return 0;
    }
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        Log.d("myAppName", "Error: external storage is read only.");
        return 0;
    }
    Log.d("myAppName", "External storage is not read only or unavailable");

    if (ContextCompat.checkSelfPermission(this, // request permission when it is not granted.
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        Log.d("myAppName", "permission:WRITE_EXTERNAL_STORAGE: NOT granted!");
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),folderName);
    int result = 0;
    if (folder.exists()) {
        Log.d("myAppName","folder exist:"+folder.toString());
        result = 2; // folder exist
    }else{
        try {
            if (folder.mkdirs()) {
                Log.d("myAppName", "folder created:" + folder.toString());
                result = 1; // folder created
            } else {
                Log.d("myAppName", "creat folder fails:" + folder.toString());
                result = 0; // creat folder fails
            }
        }catch (Exception ecp){
            ecp.printStackTrace();
        }
    }
    return result;
  }

我正在尝试在Android上保存屏幕截图,以便它将出现在手机照片库中。文件路径创建得很好,但用于保存屏幕截图的try / catch语句总是以未保存的文件结束。我正在使用Log.d来返回代码中发生的事情。我试图发布Logcat的截图,但显然我没有足够的声誉来做这些事情......

我正在使用物理设备来测试应用程序。我还要求在清单中写入外部媒体。我现在有点陷入困境。

更新:

 @Override
        public void onClick(View OnclickView) {

            view = OnclickView.getRootView();

            view.setDrawingCacheEnabled(true);

            bitmap = view.getDrawingCache();

           // ScreenShotHold.setImageBitmap(bitmap);


            String foldername = "WhatsThis";

            mkFolder(foldername);

            File file = mkFolder(foldername);


            try
            {
                file.createNewFile();
                fileoutputstream = new FileOutputStream(file);
                fileoutputstream.write(bytearrayoutputstream.toByteArray());
                fileoutputstream.close();
                Log.d("WhatsThisApp","Screenshot Saved!");

            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d("WhatsThisApp","Screenshot not Saved!");
            }

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri screenshotUri = Uri.parse("android.resource://" + getPackageName()
                    + "/drawable/" + "ic_launcher");
            sharingIntent.setAction(Intent.ACTION_SEND);

            sharingIntent.setType("image/jpeg");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
            sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));
        }
    });

}


public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
public File mkFolder(String folderName){ // make a folder under Environment.DIRECTORY_DCIM
    String state = Environment.getExternalStorageState();
    int result = 0;
    if (!Environment.MEDIA_MOUNTED.equals(state)){
        Log.d("myAppName", "Error: external storage is unavailable");
        result = 0;
    }
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        Log.d("myAppName", "Error: external storage is read only.");
        result = 0;
    }
    Log.d("myAppName", "External storage is not read only or unavailable");


    File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),folderName);

    if (folder.exists()) {
        Log.d("myAppName","folder exist:"+folder.toString());
        result = 2; // folder exist
    }else{
        try {
            if (folder.mkdirs()) {
                Log.d("myAppName", "folder created:" + folder.toString());
                result = 1; // folder created
            } else {
                Log.d("myAppName", "creat folder fails:" + folder.toString());
                result = 0; // creat folder fails
            }
        }catch (Exception ecp){
            ecp.printStackTrace();
        }
    }
    return folder;
}

好的......我还在记录“说截图没有保存!” 已启用应用程序存储权限。

这是我的LOGS

11-26 16:56:13.527 28399-28454/? I/OpenGLRenderer: Initialized EGL, version 1.4
11-26 16:56:13.527 28399-28454/? D/OpenGLRenderer: Swap behavior 1
11-26 16:56:13.541 28399-28449/? W/AnalyticsUserIDStore: initStore should have been called before calling setUserID
11-26 16:56:16.580 28399-28399/edu.selu.teamtron.whatsthis2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-26 16:56:16.662 28399-28399/edu.selu.teamtron.whatsthis2 I/WhatsThisAppLogging: In the onCreate() method of the WhatsThisAPPActivity Class
11-26 16:56:16.662 28399-28399/edu.selu.teamtron.whatsthis2 D/MainActivity: onCreate(Bundle) called
11-26 16:56:16.664 28399-28399/edu.selu.teamtron.whatsthis2 D/WhatsThisAppLogging: onStart() called
11-26 16:56:16.666 28399-28399/edu.selu.teamtron.whatsthis2 D/WhatsThisAppLogging: onResume() called
11-26 16:56:28.119 28399-28399/edu.selu.teamtron.whatsthis2 D/myAppName: External storage is not read only or unavailable
11-26 16:56:28.121 28399-28399/edu.selu.teamtron.whatsthis2 D/myAppName: folder exist:/storage/emulated/0/Pictures/WhatsThis
11-26 16:56:28.127 28399-28399/edu.selu.teamtron.whatsthis2 D/myAppName: External storage is not read only or unavailable
11-26 16:56:28.129 28399-28399/edu.selu.teamtron.whatsthis2 D/myAppName: folder exist:/storage/emulated/0/Pictures/WhatsThis
11-26 16:56:28.130 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/WhatsThis (Is a directory)
11-26 16:56:28.130 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at java.io.FileOutputStream.open(Native Method)
11-26 16:56:28.130 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
11-26 16:56:28.130 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
11-26 16:56:28.130 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at edu.selu.teamtron.whatsthis2.MainActivity$2.onClick(MainActivity.java:185)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.view.View.performClick(View.java:5612)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.view.View$PerformClick.run(View.java:22285)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.os.Looper.loop(Looper.java:154)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6154)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
11-26 16:56:28.131 28399-28399/edu.selu.teamtron.whatsthis2 D/WhatsThisApp: Screenshot not Saved!
11-26 16:56:28.144 28399-28399/edu.selu.teamtron.whatsthis2 D/WhatsThisAppLogging: onPause() called

0 个答案:

没有答案