2016-11-22 104 views
0

此代碼在6.0.1安卓版本下正常工作,但如果我在6.0.1安卓設備上運行此應用程序,它不會將圖像保存到SD卡。 我需要更新6.0.1設備?保存到SD卡的6.0.1 Android版本

public void SaveImages(int a ,String b) 
     { 
      Bitmap bitmap = null; 
      OutputStream output; 
      if(a==0) 
      { 
       bitmap = BitmapFactory.decodeResource(getResources(), 
         R.drawable.image_0); 
      } 



File filepath = Environment.getExternalStorageDirectory(); 

     // Create a new folder in SD Card 
     File dir = new File(filepath.getAbsolutePath() 
       + "/Wallpapers/"); 
     dir.mkdirs(); 

     // Create a name for the saved image 
     File file = new File(dir,b); 

     // Show a toast message on successful save 
     Toast.makeText(FullImageActivity.this, "Loading...", 
       Toast.LENGTH_SHORT).show(); 
     Toast.makeText(FullImageActivity.this, "Image Saved to SD Card", 
       Toast.LENGTH_SHORT).show(); 
     try { 

      output = new FileOutputStream(file); 

      // Compress into png format image from 0% - 100% 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); 
      output.flush(); 
      output.close(); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); 
     } 

     catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

這將引發安全異常Android中6 ....第一次請求的許可,則保存它會工作 –

+0

檢查我只是說此權限的應用程序 – Nithinlal

+0

的運行permision: <使用許可權android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> – Latmos

回答

3

在Android 6.0以上版本,你需要request runtime permission to write to external storage.


爲了請求運行權限寫入外部存儲:

public class MarshmallowPermission { 
    public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2; 

    public MarshmallowPermission() { 
    } 

    public boolean checkPermissionForExternalStorage(Activity activity) { 
     if(Build.VERSION.SDK_INT >= 23) { 
      int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
      if(result == PackageManager.PERMISSION_GRANTED) { 
       return true; 
      } else { 
       return false; 
      } 
     } else { 
      return true; 
     } 
    } 

    public void requestPermissionForExternalStorage(Activity activity) { 
     if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
      Toast.makeText(activity, 
        "External Storage permission needed. Please allow in App Settings for additional functionality.", 
        Toast.LENGTH_LONG).show(); 
      // user has previously denied runtime permission to external storage 
     } else { 
      ActivityCompat.requestPermissions(activity, 
        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE); 
     } 
    } 
} 

然後,你可以做

if(!marshmallowPermission.checkPermissionForExternalStorage(this)) { 
    marshmallowPermission.requestPermissionForExternalStorage(this); 
} else { 
    // can write to external 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(requestCode == MarshmallowPermission.EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE) { 
     if(marshmallowPermission.checkPermissionForExternalStorage(this)) { 
      // can write to external 
     } else { 
      // runtime permission denied, user must enable permission manually 
     } 
    } 
} 
+0

Thanks.It工程。 – Latmos