保存按钮不起作用

时间:2013-06-07 10:06:03

标签: android file bitmap

我在返回结果时有位图图像,然后我想将它保存在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();
        }
    }
});

3 个答案:

答案 0 :(得分: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();

                        }


                    }
                });

答案 1 :(得分: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();

OR

  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();
             }

答案 2 :(得分: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();
                }