拡張子:txtvbswsfjsphpjavahtmlutf8sjis public class MainActivity extends AppCompatActivity { // ダウンロード用 private FirebaseStorage storage; private StorageReference storageRef; private StorageReference imageRef; private ImageView imageView; private File galleryFile; private String imagePath; private ProgressDialog progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ▼ テスト用のルール // service firebase.storage { // match /b/freebase-654b7.appspot.com/o { // match /{allPaths=**} { // allow read; // allow write: if true; // } // } // } // ダウンロードするファイル storage = FirebaseStorage.getInstance(); storageRef = storage.getReferenceFromUrl("gs://freebase-654b7.appspot.com/"); imageRef = storageRef.child("sworc2.png"); // 表示する場所 imageView = (ImageView) MainActivity.this.findViewById(R.id.imageView); // ダウンロード中のダイアログ progress = new ProgressDialog(MainActivity.this); // ************************************* // ギャラリーにダウンロードして表示 // ************************************* Button galleryButton = (Button) MainActivity.this.findViewById(R.id.galleryButton); galleryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ダウンロード中の表示 progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); progress.setMessage("画像をダウンロードしています"); progress.show(); // ギャラリー用に内部ストレージにフォルダを作成 String firebaseImageDir = Environment.getExternalStorageDirectory().getPath() + "/firebase"; File file = new File(firebaseImageDir); // ディレクトリ初期作成 if (!file.exists()) { if (file.mkdir() == false) { Log.i("lightbox", "ディレクトリを作成できませんでした"); return; } } // ギャラリー用画像保存パス Calendar cal = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd_HHmmss"); imagePath = firebaseImageDir + "/" + sf.format(cal.getTime()) + ".png"; galleryFile = new File(imagePath); // ファイルにダウンロード imageRef.getFile(galleryFile) .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { // ダウンロード中の表示解除 progress.dismiss(); // ギャラリーに反映 MediaScannerConnection.scanFile( MainActivity.this, new String[] { imagePath }, new String[] { "image/png" }, null); // ImageView に表示 FileInputStream fis = null; try { fis = new FileInputStream(galleryFile); if ( fis != null ) { Bitmap image = BitmapFactory.decodeStream(fis); imageView.setImageBitmap(image); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.i("lightbox","ダウンロードできませんでした"); Log.i("lightbox",e.getMessage()); // ダウンロード中の表示解除 progress.dismiss(); // メッセージ表示 Toast.makeText(MainActivity.this,"ダウンロードに失敗しました",Toast.LENGTH_LONG).show(); } }); } }); } }
public class MainActivity extends AppCompatActivity { // ダウンロード用 private FirebaseStorage storage; private StorageReference storageRef; private StorageReference imageRef; private ImageView imageView; private File galleryFile; private String imagePath; private ProgressDialog progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ▼ テスト用のルール // service firebase.storage { // match /b/freebase-654b7.appspot.com/o { // match /{allPaths=**} { // allow read; // allow write: if true; // } // } // } // ダウンロードするファイル storage = FirebaseStorage.getInstance(); storageRef = storage.getReferenceFromUrl("gs://freebase-654b7.appspot.com/"); imageRef = storageRef.child("sworc2.png"); // 表示する場所 imageView = (ImageView) MainActivity.this.findViewById(R.id.imageView); // ダウンロード中のダイアログ progress = new ProgressDialog(MainActivity.this); // ************************************* // ギャラリーにダウンロードして表示 // ************************************* Button galleryButton = (Button) MainActivity.this.findViewById(R.id.galleryButton); galleryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ダウンロード中の表示 progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); progress.setMessage("画像をダウンロードしています"); progress.show(); // ギャラリー用に内部ストレージにフォルダを作成 String firebaseImageDir = Environment.getExternalStorageDirectory().getPath() + "/firebase"; File file = new File(firebaseImageDir); // ディレクトリ初期作成 if (!file.exists()) { if (file.mkdir() == false) { Log.i("lightbox", "ディレクトリを作成できませんでした"); return; } } // ギャラリー用画像保存パス Calendar cal = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd_HHmmss"); imagePath = firebaseImageDir + "/" + sf.format(cal.getTime()) + ".png"; galleryFile = new File(imagePath); // ファイルにダウンロード imageRef.getFile(galleryFile) .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { // ダウンロード中の表示解除 progress.dismiss(); // ギャラリーに反映 MediaScannerConnection.scanFile( MainActivity.this, new String[] { imagePath }, new String[] { "image/png" }, null); // ImageView に表示 FileInputStream fis = null; try { fis = new FileInputStream(galleryFile); if ( fis != null ) { Bitmap image = BitmapFactory.decodeStream(fis); imageView.setImageBitmap(image); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.i("lightbox","ダウンロードできませんでした"); Log.i("lightbox",e.getMessage()); // ダウンロード中の表示解除 progress.dismiss(); // メッセージ表示 Toast.makeText(MainActivity.this,"ダウンロードに失敗しました",Toast.LENGTH_LONG).show(); } }); } }); } }