Using SDCard

per Jordi Farrero darrera modificació 2020-03-25T14:54:40+01:00

INTENTEM FER UN SCREENSHOT DE LA PANTALLA:

(BUSCAR COM FER UNA CAPTURA I QUE RETORNI UN BMP) 

RETORNA UN BMP (Anomenat pBitmap) I ARA TOCA TIRAR-LO A LA SDCARD (CAL POSAR PERMISOS AL XML)


ByteArrayOutputStream bytes = new ByteArrayOutputStream();
pBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

File directory=null;

if (isExternalSDCard())
{
directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+File.separator+"Captura");
if (!directory.isDirectory() && !directory.exists()) directory.mkdirs();
}
else
{
directory = new File(Environment.getDataDirectory() + Environment.DIRECTORY_DCIM + File.separator+"Captura");
if (!directory.isDirectory() && !directory.exists()) directory.mkdirs();
}

File f = null;

if (game_commons.isExternalSDCard())
{
FileName="Captura" + System.currentTimeMillis() + ".png";
FullURI=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Captura/" + FileName;;
f = new File(FullURI);

MediaScannerConnection.scanFile(game_commons.gActivity,new String[] { FullURI },null,new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path,Uri uri)
{
}});

}
else
{
FileName="Captura" + System.currentTimeMillis() + ".png";
FullURI=Environment.DIRECTORY_DCIM + "/Captura/" + FileName;
f = new File(Environment.getDataDirectory() + FullURI);
MediaScannerConnection.scanFile(ACTIVITY,new String[] { FullURI },null,new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path,Uri uri)
{
}});
}

try
{
f.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}

FileOutputStream fo = null;
try
{
fo = new FileOutputStream(f);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
fo.write(bytes.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
}

// remember close de FileOutput
try
{
fo.close();
}
catch (IOException e)
{
e.printStackTrace();
}

ACTIVITY.runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (isExternalSDCard())
{
Toast.makeText(ACTIVITY,"PHOTO NAME -> "+ FileName,Toast.LENGTH_LONG).show();

}
else
{
Toast.makeText(ACTIVITY,"PHOTO NAME -> "+ FileName,Toast.LENGTH_LONG).show();
}
}
});

 

//----------------------------------------------------- FUNCIO NECESSARIA -----------------------------------------------------------

public static boolean isExternalSDCard()
{
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
return mExternalStorageAvailable && mExternalStorageWriteable;
}