0

I'm using this java code to compress an image in basic4android! However, nothing works!

#if java
import android.graphics.Bitmap;
import java.io.*;
import android.os.Environment;
public void Resize(Bitmap mPhoto)
{
    try
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        mPhoto.compress(Bitmap.CompressFormat.PNG, 50, bytes);

        File file = new File(Environment.getExternalStorageDirectory()+"/"+"11111.jpg");
        file.createNewFile();
        FileOutputStream fo = new FileOutputStream(file);
        fo.write(bytes.toByteArray());
        fo.close();
    }
    catch (Exception e) {}
}
#end if

No file is shown under ExternalStorageDirectory.

Lamorak
  • 10,957
  • 9
  • 43
  • 57
Salar Ashgi
  • 128
  • 2
  • 13

1 Answers1

0

Try first to create a directory in the root directory and then write the file in it like this for example:

File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + “/your-dir-name/”);
dir.mkdir();
File file = new File(dir, “11111.jpg”);
FileOutputStream os = outStream = new FileOutputStream(file);
os.write(bytes.toByteArray());
os.close();

And please do not forget to add this permission to your AndroidManifest-file

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30