-1

I got an app on which you can put your signature. I'm having trouble with saving the image. I would expect the file to be saved in sdcard/signature, because thats the folder i specified in strings.xml and the folder is created. But instead it saved the image to the DCIM/camera folder and not in the sdcard/signature folder.

I already read about deleting the image from the DCIM folder, so thats not a problem. But i would really appriciate if someone can tell me why the image is not saving in sd/signature

In onCreate this happens (external dir is just a name like StoreSignature for a folder to store the images):

 tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/";
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE);

This is the save method:

mGetSign.setOnClickListener(new OnClickListener() 
    {        
        public void onClick(View v) 
        {
            Log.v("log_tag", "Panel Saved");
            boolean error = captureSignature();
            if(!error){
                mView.setDrawingCacheEnabled(true);
                mSignature.save(mView);
                Bundle b = new Bundle();

                String status = "done";
                File path = mypath;
                Log.i("info4", path.toString());
                String[] arr={status, path.toString()};
                b.putStringArray("status", arr);
                Log.i("info5", arr.toString());
                Intent intent = new Intent();
                intent.putExtras(b);
                setResult(RESULT_OK,intent);   
                finish();
            }
        }
    });

finally this is where the folders are prepared (dont know if this had to do with is)

private boolean prepareDirectory() 
{
    try
    {
        if (makedirs()) 
        {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) 
    {
        e.printStackTrace();
        Toast.makeText(this, "Kan het file systeem niet vinden.. Zit de SD-kaart er goed in?", 1000).show();
        return false;
    }
}

/**
 * Creates a directory
 * 
 * @return whether the directory is created or not
 */
private boolean makedirs() 
{
    File tempdir = new File(tempDir);
    if (!tempdir.exists())
        tempdir.mkdirs();

    if (tempdir.isDirectory()) 
    {
        File[] files = tempdir.listFiles();
        for (File file : files) 
        {
            if (!file.delete()) 
            {
                System.out.println("Deleten is mislukt " + file);
            }
        }
    }
    return (tempdir.isDirectory());
}

Strings.xml (signature values)

<string name="cancel">Annuleren</string>
<string name="clear">Opnieuw</string>
<string name="save">Opslaan</string>

<string name="naam">Naam</string>
<string name="external_dir">Handtekening</string>
<string name="input"></string>
<string name="title_activity_signature">Handtekening</string>

Is there anyone who sees what i'm doing wrong?

Thanks in advance!

GeertG
  • 158
  • 2
  • 12
  • Can you show your strings XML? – andy256 Sep 27 '13 at 06:13
  • ofcourse, i'm adding it now. Only the signature part will do it? because my strings.xml is kinda big. – GeertG Sep 27 '13 at 06:15
  • 1
    Use the below link. It will solve your problem [Android saving file to external storage][1] [1]: http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage – Ameer Sep 27 '13 at 06:24
  • Thanks, it works! Only had to change jpeg compress to PNG compress. I tryed that solution before and got a black image, but it works now – GeertG Sep 27 '13 at 06:46

2 Answers2

0

Try the below link. First of all you should check weather external storage is available or not. It will solve your problem

Android saving file to external storage

check all the answers in this link

Community
  • 1
  • 1
Ameer
  • 2,709
  • 1
  • 28
  • 44
0

Try this code :

private void SaveIamge(Bitmap finalBitmap) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");    
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 
        try {
               FileOutputStream out = new FileOutputStream(file);
               finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();

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

Also put the permission to manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Arun PS
  • 4,610
  • 6
  • 41
  • 56