将图像添加到SDCARD中的文件夹

时间:2015-03-19 06:20:39

标签: android android-studio

每次捕获图像时,文件夹创建部分工作正常,但图像未添加到文件夹

photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) 
{
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode != RESULT_CANCELED) 
{
folder = new File(Environment.getExternalStorageDirectory() + File.separator+"folder/");                          

if(!folder.exists())

        {
            folder.mkdirs();
            Log.d("SDcard", "Folder created");
        }
        else
        {
            Log.d("SDCard", "Folder already exists");
        }
File file = new File(Environment.getExternalStorageDirectory() +        File.separator +"folder/");
        Uri photoPath = Uri.fromFile(file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);    ` 
        }
        }

如果我直接添加文件夹创建并将图像逻辑保存到onclick(),则会显示错误,指出“无法提供结果信息”

*请帮助*

2 个答案:

答案 0 :(得分:1)

public void saveBitmapToFile(Bitmap bmp) {

        File mAppBaseDir;
        if (isExternalStorageWritable())
            mAppBaseDir = new File(Environment.getExternalStorageDirectory(), "FolderName");
        else
            mAppBaseDir = new File(getApplicationContext().getFilesDir().getParent()).getAbsoluteFile();

        if (!mAppBaseDir.exists()) {
            mAppBaseDir.mkdirs();
        }
        File imageDir = new File(mAppBaseDir, "Profile");
        if (!imageDir.exists())
            imageDir.mkdirs();
        File file = new File(imageDir + "/" + "profile.png");
        if (file.exists()) {
            file.delete();
        }
        try {
            writeBytesToFile(file, bitmapToByte(bmp));

        } catch (IOException e) {
            // show alert for retry choose photo
            e.printStackTrace();
        }
    }



public void writeBytesToFile(File file, byte[] bytes) throws IOException {
        BufferedOutputStream bos = null;

        try {
            FileOutputStream fos = new FileOutputStream(file.getPath());
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            Log.e("", e.getMessage());
        } finally {
            if (bos != null) {
                try {
                    bos.flush();
                    bos.close();
                } catch (Exception e) {
                    Log.e("", e.getMessage());
                }
            }
        }
    }

public byte[] bitmapToByte(Bitmap bitmapFinally) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmapFinally.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }

答案 1 :(得分:0)

我根据您的评论回答这个问题"请您详细说明" 。 CMIIW:

photoButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        folder = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/");

        if (!folder.exists())

        {
            folder.mkdirs();
            Log.d("SDcard", "Folder created");
        } else {
            Log.d("SDCard", "Folder already exists");
        }
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/");
        Uri photoPath = Uri.fromFile(file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
});

"我已经在oncreate()方法" 之前声明了cameraIntent对象,这对此没有任何问题,但是在调用startActivityForResult后会触发onActivityResult ,然后意图不会有额外的你给予