۱۳۹۴/۰۴/۲۰, ۰۷:۰۱ ق.ظ
کد پیاچپی:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String position_string = "you pressed the item number " + String.valueOf(position+1) + " in GridView";
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/myFolder");
dir.mkdirs(); // build directory
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fileName = "Image-"+ n +".jpg";
File file = new File(dir, fileName);
if (file.exists ()) file.delete ();
try {
FileOutputStream outStream = new FileOutputStream(file);
InputStream is;
Bitmap bitmap;
is = this.getResources().openRawResource(R.drawable.image_1);
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
is = null;
} catch (IOException e) {
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, outputFileUri);
startActivity(Intent.createChooser(intent, "Share this image via"));
}