简体   繁体   中英

android saving to SD card

hi to all i have a small problem

i have this code to save an image to an SD card

public String SDSave( ) { //View arg0
                // TODO Auto-generated method stub
                OutputStream outStream = null;
                File file = new File( extStorageDirectory , AdName + ".PNG");
                try {
                    outStream = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                    outStream.flush();
                    outStream.close();

                    Toast.makeText(WhereAmI.this, "Saved", Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(WhereAmI.this, e.toString(), Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(WhereAmI.this, e.toString(), Toast.LENGTH_LONG).show();
                }

            }

and im calling it from another methode like this:

String sdSave = SDSave(extStorageDirectory + "/qr11.PNG");

and im showing 2 errors the first is at the method at public String SDSave( ) and it says:

This method must return a result of type String

and the second is at String sdSave = SDSave(extStorageDirectory + "/qr11.PNG"); and it says:

The method SDSave() in the type WhereAmI is not applicable for the arguments (String)

some help please on how to fix it.....

thank you

You declared your method as public String which means it has to return a String object from the function (using the return keyword) if you don't want the function to return anything, use the void keyword instead of String .

The second error means that somewhere you called SDSave method using a String as arguments ( SDSave(extStorageDirectory + "/qr11.PNG"); ) while this function takes no arguments ( public String SDSave( ) ).

I suggest you would try a Java tutorial or two , since those rules are the very basics of Java (and actually, of many programing languages).

Your asking your method to return a String

 public String SDSave()

you would need the line, at the end of your code:

return "someString"; 

If you don't want to return a string use:

public void SDSave()

The second error, your trying to pass a String into your method, but your method doesn't take any variables. You would need:

public void SDSave(String myInputStringVariable)

Yes these are Java basic's , so I'd go read up on Java and OO, also check this out:

Top Ten Errors Java Programmers Make

try writing this line in the manifest :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope it will help u.

public void SDSave(String file){
    File f=new File(file);
    ...
}

Hey You can use my sample code shared on Github repo link This sample contains :

  1. Download image from web.
  2. You can also share this image.
  3. You can save this image on Sd card.
  4. Image Caching is also there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM