图像文件夹不节能 [英] Image not saving in folder

查看:164
本文介绍了图像文件夹不节能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个文件夹并保存图像了。结果
但它不工作。结果
我不知道什么是错在我的code - ?你能告诉我,为什么

  //用于调用上传图片的方法
公共无效openGallery(){
    意向意图=新的Intent();
    intent.setType(图像/ *);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(意向,选择图片),1);}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){    super.onActivityResult(要求code,结果code,数据);    如果(要求code == 1安培;&安培;结果code == RESULT_OK&放大器;&放大器;数据= NULL&放大器;!&安培;!data.getData()= NULL){        文件夹=新的文件(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES),ALBUMNAME);
        档案文件=新的文件(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES),文件名+3);
        。位图imageToSave =(位图)data.getExtras()获得(数据);
        尝试{
            FileOutputStream中出=新的FileOutputStream(文件);
            imageToSave.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
            了out.flush();
            out.close();
        }赶上(例外五){
            e.printStackTrace();
        }
        乌里selectedImage = data.getData();
        意图I =新意图(这一点,
                AddImage.class);
        i.putExtra(的ImagePath,selectedImage.toString());
        startActivity(ⅰ);
    }

修改

 最后文件路径=
                Environment.getExternalStoragePublicDirectory
                        (
                              // Environment.DIRECTORY_PICTURES +/ SS /
                                //Environment.DIRECTORY_DCIM
                               Environment.DIRECTORY_DCIM +/ MyFolderName /
                        );        //确保图片目录存在。
        如果(!path.exists())
        {
            path.mkdirs();
        }
        。位图imageToSave =(位图)data.getExtras()获得(数据);
        最终文件的文件=新的文件(路径,文件+.JPG);
        尝试{
             FOS的FileOutputStream =新的FileOutputStream(路径);
            最终的BufferedOutputStream BOS =新的BufferedOutputStream(FOS,8192);            FileOutputStream中出=新的FileOutputStream(路径);
            // FOS =新的FileOutputStream(路径);
            imageToSave.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
           // imageToSave.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
            了out.flush();
            out.close();
        }赶上(例外五){
            e.printStackTrace();
        }


解决方案

我如何使DCIM文件夹并没有创建一个文件到:

  / *
    创建,我们将会把我们的图片在用户的公共路径
    图片目录。需要注意的是,你应该小心你
    放置在这里,由于用户经常管理这些文件。
    对于属于应用程序的图片和其他媒体,考虑
    Context.getExternalMediaDir()。
    * /
    最终的文件路径=
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            //Environment.DIRECTORY_DCIM
            Environment.DIRECTORY_DCIM +/ MyFolderName /
        );    //确保图片目录存在。
    如果(!path.exists())
    {
        path.mkdirs();
    }    最终文件的文件=新的文件(路径,fileJPG +.JPG);    尝试
    {
        最终的FileOutputStream FOS =新的FileOutputStream(文件);
        最终的BufferedOutputStream BOS =新的BufferedOutputStream(FOS,8192);        //bmp.com$p$pss(Com$p$pssFormat.JPEG,100,BOS);
        bmp.com preSS(比较pressFormat.JPEG,85,BOS);        bos.flush();
        bos.close();
    }
    赶上(最终IOException异常E)
    {
        e.printStackTrace();
    }

fileJPG 是我创建的(动态,增加一个日期)的文件名。结果
替换 MyFolderName ALBUMNAME 。结果
BMP 是我的位图数据(屏幕截图,在我的情况)。

I am trying to create a folder and save images in it.
But it's not working.
I don't know what's wrong in my code - can you tell me why?

    // The method that invoke of uploading images
public   void openGallery() {


    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

        File folder = new File(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), "albumName");


        File file = new File(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), "fileName"+3);
        Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri selectedImage = data.getData();


        Intent i = new Intent(this,
                AddImage.class);
        i.putExtra("imagePath", selectedImage.toString());
        startActivity(i);


    }

edit

        final File path =
                Environment.getExternalStoragePublicDirectory
                        (
                              //  Environment.DIRECTORY_PICTURES + "/ss/"
                                //Environment.DIRECTORY_DCIM
                               Environment.DIRECTORY_DCIM + "/MyFolderName/"
                        );

        // Make sure the Pictures directory exists.
        if(!path.exists())
        {
            path.mkdirs();
        }
        Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
        final File file = new File(path, "file" + ".jpg");
        try {
             FileOutputStream fos = new FileOutputStream(path);
            final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

            FileOutputStream out = new FileOutputStream(path);
            //fos = new FileOutputStream(path);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
           // imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

解决方案

How I do make the folder in DCIM and create a file there into:

    /*
    Create a path where we will place our picture in the user's public
    pictures directory. Note that you should be careful about what you
    place here, since the user often manages these files.
    For pictures and other media owned by the application, consider
    Context.getExternalMediaDir().
    */
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            //Environment.DIRECTORY_DCIM
            Environment.DIRECTORY_DCIM + "/MyFolderName/"
        );

    // Make sure the Pictures directory exists.
    if(!path.exists())
    {
        path.mkdirs();
    }

    final File file = new File(path, fileJPG + ".jpg");

    try
    {
        final FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

        //bmp.compress(CompressFormat.JPEG, 100, bos);
        bmp.compress(CompressFormat.JPEG, 85, bos);

        bos.flush();
        bos.close();
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }

fileJPG is the file name I'm creating (dynamically, adding a date).
Replace MyFolderName with albumName.
bmp is my Bitmap data (a screenshot, in my case).

这篇关于图像文件夹不节能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆