2016-08-16 38 views
0

我已經設置了應用程序標記之外的所有uses-permission設置我的Android manifest文件裏面這裏的屏幕:我得到java.io.FileNotFoundException時,試圖捕捉

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

但是當我嘗試捕捉屏幕我收到以下錯誤:

java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/EMOJI_2-7-116_22538.jpg: open failed: EACCES (Permission denied)

這是捕獲屏幕的方法。

private void captureScreen() { 
     Date now = new Date(); 
     now.getYear(); 
     now.getMonth(); 
     now.getDay(); 

     View v = findViewById(R.id.rl); 
     v.setDrawingCacheEnabled(true); 
     Bitmap bitmap = v.getDrawingCache(); 
     String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) 
       + File.separator + "Camera" + File.separator + "EMOJI_" + 
       now.getDay() + "-" + now.getMonth() + "-" + now.getYear() + "_" + now.getHours() + now.getMinutes() + now.getSeconds() + ".jpg"; 
     File file = new File(dest); 
     try { 
      FileOutputStream stream = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
      stream.flush(); 
      stream.close(); 
      Toast.makeText(getApplicationContext(), "Saved !", Toast.LENGTH_SHORT).show(); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "An error occured", Toast.LENGTH_LONG).show(); 
     } finally { 
      v.setDrawingCacheEnabled(false); 
     } 

     // Scan the image to make it appear in gallery 
     MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null); 
    } 

幫幫我如何解決這個問題?

+0

首先你可以檢查你從dest到目標 –

+0

從dest到達後的字符串:「/storage/emulated/0/DCIM/Camera/EMOJI_2-7-116_22538.jpg」 –

+0

嘗試添加讀權限「READ_EXTERNAL_STORAGE」。你的應用程序的目標是什麼API版本? – HeatfanJohn

回答

0

您正試圖訪問系統中當前不存在的文件。

而是使用..

OutputStream out = new FileoutputStream(dest); 

//用字符串這一次實例化FileOutputStream對象的對象不是這樣

File sd = Environment.getExternalStorageDirectory(); 
File image = new File(sd+filePath, imageName); 
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); 
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); 
imageView.setImageBitmap(bitmap); 

可能是幫助全給你文件對象

+0

補全廢話。他正在努力創造一個。沒有訪問現有的。 – greenapps

0

嘗試。

+0

我已經嘗試了所有的解決方案,但沒有任何工作,我的目標sdk版本是24。 –