0

返回结果时我有位图图像,然后我想将其保存在 SD 卡的 Photo Art Camera 文件夹中,但未保存。它显示吐司“照片未成功保存”。

这是代码:

mSave.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        File cacheDir;
        Bitmap bitmap = result;

        // String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        Date d = new Date();

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "Photo Art Camera");
        } else { cacheDir = MainActivity.this.getCacheDir(); }

        if (!cacheDir.exists()) { cacheDir.mkdirs(); }

        File file = new File(cacheDir, "PhotoMarge" + d.getTime() + ".jpg");
        try {
            fOut = new FileOutputStream(file);
            bitmap.compress(CompressFormat.PNG, 150, fOut);
            // getImageBitmap(myurl).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();

            Toast.makeText(MainActivity.this, "Photo Saved Sucessfully", 500).show();

            // mDialog.dismiss();
            // MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
        }
        catch (FileNotFoundException e) { e.printStackTrace(); }
        catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Photo Not Saved Sucessfully", 500).show();
        }
    }
});
4

3 回答 3

2
i removed my misstake from code .Actually i am saving another bitmap instead saving and converting framelayout bitmap
File cacheDir;
                        frame.setDrawingCacheEnabled(true);

                        icon = Bitmap.createBitmap(frame.getDrawingCache());

                        Bitmap bitmap = icon;

                    //  String path = Environment.getExternalStorageDirectory().toString();
                        OutputStream fOut = null;
                        Date d=new Date();

                        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Photo Art Camera");
                        else
                            cacheDir=MainActivity.this.getCacheDir();
                        if(!cacheDir.exists())
                            cacheDir.mkdirs();

                        File file = new File(cacheDir, "PhotoMarge"+d.getTime()+".jpg");
                        try {
                            fOut = new FileOutputStream(file);
                            bitmap.compress(CompressFormat.PNG, 100, fOut);
                            //getImageBitmap(myurl).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            fOut.flush();
                            fOut.close();
                            count();
                            Toast.makeText(MainActivity.this, "Photo Saved Sucessfully", 500).show();

                        //  mDialog.dismiss();

                            //MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "Photo Not Saved Sucessfully", 500).show();

                        }


                    }
                });
于 2013-06-20T05:40:14.637 回答
1

您正在使用不同的参数对其进行压缩,使其达到 100 或 120 它应该可以工作..

OutputStream fOut = new FileOutputStream(output);
merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();

Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();

或者

  String strMyImagePath = f.getAbsolutePath();
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(f);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

                 e.printStackTrace();
             } catch (Exception e) {

                 e.printStackTrace();
             }
于 2013-06-19T15:47:10.353 回答
0
save22.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                File cacheDir;
                Toast.makeText(Main.this, "Photo", 500)
                .show();
                Bitmap icon;
                relativelayout.setDrawingCacheEnabled(true);

                icon = Bitmap.createBitmap(relativelayout.getDrawingCache());
                Bitmap bitmap = icon;
                relativelayout.setDrawingCacheEnabled(false);
                //File mFile1 = Environment.getExternalStorageDirectory();
                Date d=new Date();
                String fileName = d.getTime()+"mg1.jpg";


                File storagePath = (Environment
                        .getExternalStorageDirectory());
                File dest = new File(storagePath + "/CityAppImages");

                if (!dest.exists()) {
                    dest.mkdirs();

                }

                File mFile2 = new File(dest, fileName);
                sdpath= mFile2.getAbsolutePath();

                Log.d("qqqqqqqqqqqqqqqqqqqqqqq", "zzzzzzzz" + sdpath);
                try {
                    FileOutputStream outStream;

                    outStream = new FileOutputStream(mFile2);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

                    outStream.flush();

                    outStream.close();
                    Toast.makeText(Main.this, "Photo Saved Sucessfully", 500)
                    .show();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(Main.this, "Photo Not Saved Sucessfully",
                            500).show();
                }
于 2013-07-23T07:20:08.623 回答