Problem in saving image

Problems with Canvas, OpenGL, etc...

Problem in saving image

Postby rjuy » Thu Apr 28, 2011 7:20 am

Hello,

Can someone help me with this.

I have a code in saving an image, but when I'm about to save the image permission is denied.

please see code below.

Code: Select all
package appiction.com;

import java.io.*;
import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class SaveImage extends GraphicsActivity {
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this)); 
    }
   
    public static class SampleView extends View {
        private static final int WIDTH = 5;
        private static final int HEIGHT = 5;
        private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
        private final Bitmap mBitmap;
        private final float[] mVerts = new float[COUNT*2];
        private final float[] mOrig = new float[COUNT*2];
        private int imgCtr = 0;
        private static void setXY(float[] array, int index, float x, float y) {
            array[index*2 + 0] = x;
            array[index*2 + 1] = y;
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
           
            mBitmap = BitmapFactory.decodeResource(getResources(),
                                                     R.drawable.bcimage1);
            float w = mBitmap.getWidth();
            float h = mBitmap.getHeight();
           
            // construct our mesh
            int index = 0;
            for (int y = 0; y <= HEIGHT; y++) {
                float fy = h * y / HEIGHT;
                for (int x = 0; x <= WIDTH; x++) {
                    float fx = w * x / WIDTH;                   
                    setXY(mVerts, index, fx, fy);
                    setXY(mOrig, index, fx, fy);
                    index += 1;
                }
            }
        }
       
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);
            canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                                  null, 0, null);
       
            File myDir=new File("/sdcard/saved_images");
            myDir.mkdirs();
            imgCtr++;
            String fname = "image-" + imgCtr + ".jpg";
            File file = new File (myDir, fname);
            if (file.exists ()) file.delete ();
            saveAsJpg (file);
        }
       
        public void saveAsJpg (File f){
           String fname = f.getAbsolutePath ();
          Log.d("save","HERE ............................");
          FileOutputStream fos = null;
          try {
              fos = new FileOutputStream (f);
              mBitmap.compress (CompressFormat.JPEG, 95, fos);
              Toast.makeText(getContext(),"Saved " + fname, Toast.LENGTH_LONG).show ();
          } catch (Throwable ex) {
             Toast.makeText (getContext(), "Error: " + ex.getMessage (), Toast.LENGTH_LONG).show ();             
             ex.printStackTrace ();
          }
           
      } // end saveAsJpg
    }
}



thanks

rjuy
rjuy
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Oct 25, 2010 2:38 am

Top

Re: Problem in saving image

Postby FARRUKH ATTIQUE » Wed Aug 24, 2011 6:18 am

you have to set the user permissions in manifest file of your project at the end and before the manifest tag
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
FARRUKH ATTIQUE
Freshman
Freshman
 
Posts: 3
Joined: Wed Aug 24, 2011 6:11 am

Re: Problem in saving image

Postby raj_J2ME » Thu Aug 25, 2011 6:51 am

Yeah, as you are trying to write the image on the SDCARD and hence you need to give the permission to the app. Please let us know the issues/exact error message that you are getting on the console of logcat. So that the exact suggestion can be given t o you to follow :)
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Problem in saving image

Postby roopa » Fri Sep 23, 2011 8:09 am

I am having the exact same problem. It just started a few days ago. Can anyone help, please?
9 of 30 people found this answer helpful
roopa
Developer
Developer
 
Posts: 27
Joined: Wed Sep 14, 2011 6:46 am

Re: Problem in saving image

Postby nuetronprotron » Tue Mar 06, 2012 6:25 am

Hi rjuy,
I think the problem may be the permission
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
and one more suggestion try to use getExternalStorage() method rather than the hardcoded thing like sdcard/
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests