将图像信息位图添加到jpeg

时间:2014-05-02 07:30:04

标签: android image bitmap android-image

我正在开发一款相机应用。应用所有过滤器后,我将获得一个位图。位图还存储为jpeg图像。我正在使用以下代码。

private void saveImage(final String folderName, final String fileName, final Bitmap image) {
        File path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(path, folderName + "/" + fileName);
        try {
            file.getParentFile().mkdirs();
            image.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
            MediaScannerConnection.scanFile(mContext,
                    new String[] {
                        file.toString()
                    }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(final String path, final Uri uri) {
                            if (mListener != null) {
                                mHandler.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        mListener.onPictureSaved(uri);
                                    }
                                });
                            }
                        }
                    });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

在笔记本电脑的Windows操作系统中查看图像时保存图像。我无法获得图片的属性。无论如何,在保存图像的同时在android中添加元数据或图像信息。

1 个答案:

答案 0 :(得分:0)

用于保存文件...

public static boolean saveFile(String fileName, Context context) {
try {
    File sdDir = Environment.getExternalStorageDirectory();
    String path = sdDir.getAbsolutePath() + "/YOUR_DIR_NAME/";
    File sgDir = new File(path);
    if (!sgDir.exists()) {
        sgDir.mkdirs();
        sgDir.createNewFile();
    }
    FileWriter fw = new FileWriter(path + fileName);
    BufferedWriter out = new BufferedWriter(fw);
    String toSave = "I want to be saved in a file!";
    out.write(toSave);
    out.close();
    return true;
}
catch (Exception ex) {
    try {
        FileOutputStream os = context.openFileOutput(fileName, 0);
           String toSave = "I want to be saved in a file!";
        os.write(toSave.getBytes());
        os.flush();
        os.close();
        return true;
    }
    catch (Exception ex2) {}
}
return false;
}

使用权限: uses-permission android:name =" android.permission.WRITE_EXTERNAL_STORAGE"

用于加载文件使用此..

 String path = Environment.getExternalStorageDirectory()+ "/FileInputOutput/img1.jpg"; 
        File imgFile = new File(path);
        if(imgFile.exists())
    {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
            ImageView myImage = (ImageView) findViewById(R.id.imageView1);
            myImage.setImageBitmap(myBitmap);
    }
        else                    
            Toast.makeText(v.getContext(),"no File IS PRESENT'",Toast.LENGTH_SHORT).show();
    }