如何在android SDstorage中保存文件,但我的图像不保存在位置上

时间:2017-03-08 14:27:36

标签: android android-file

文件filepath = new File(Environment.getExternalStoragePublicDirectory(                         Environment.DIRECTORY_DOWNLOADS), “myimage.png”);

如何在android SDstorage中保存文件,但我的图像不是保存在位置上,我已尽力了 现在我假设我无法以这种方式自动创建目录。

有人可以建议如何通过代码创建目录和子目录吗?

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageButton= (ImageButton)findViewById(R.id.imageButton1);
        imageButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(isExternalStorageWriteable()){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.beautyofpakistan);

            File filepath = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS),"myimage.png");
            try {
                outputStream = new FileOutputStream(filepath);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(this,"File Saved!!",Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(this,"SDcard not supported!!",Toast.LENGTH_SHORT).show();
        }



    }
    public boolean isExternalStorageWriteable(){
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)){
            return true;
        }

0 个答案:

没有答案