Reputation: 9
I am very new to android programming. I want to design an app which will download an image in the phone's gallery and the image has already been stored in the firebase. The image is getting downloaded and is being set in the imageview also but unfortunately i can not find it in the phone gallery.I have also added write external storage permission in the manifest file.Here is my DownloadActivity.java:
package com.example.android.myapplication;
/**
* Created by Krishnendu on 25-03-2017.
*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static android.R.attr.src;
public class DownloadActivity extends RootActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView)findViewById(R.id.image);
View btnDownloadByteArray = findViewById(R.id.btn_download_byte);
btnDownloadByteArray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://test1-a7e81.appspot.com").child("as.jpg");
final long ONE_MEGABYTE = 1024 * 1024;
//download file as a byte array
storageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setImageBitmap(bitmap);
showToast("Download successful!");
}
});
}
});
View btnDownloadAsFile = findViewById(R.id.btn_download_file);
btnDownloadAsFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReferenceFromUrl("gs://test1-a7e81.appspot.com").child("as.jpg");
//get download file url
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.i("Main", "File uri: " + uri.toString());
}
});
//download the file
try {
showProgressDialog("Download File", "Downloading File...");
final File localFile = File.createTempFile("images","jpg");
storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
imageView.setImageBitmap(bitmap);
dismissProgressDialog();
showToast("Download successful!");
Log.i("path",localFile.getPath());
Log.i("parent",localFile.getParent());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
dismissProgressDialog();
showToast("Download Failed!");
}
});
} catch (Exception e ) {
e.printStackTrace();
Log.e("Main", "IOE Exception");
}
}
});
}
}
This is my RootActivity.java file:`
package com.example.android.myapplication;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class RootActivity extends AppCompatActivity {
private ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void showProgressDialog(String title, String msg) {
progressDialog = ProgressDialog.show(this, title, msg, true);
}
protected void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
protected void dismissProgressDialog() {
progressDialog.dismiss();
}
}
This is my activity_main.xml file:`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="495dp">
<Button
android:id="@+id/btn_download_byte"
android:text="Download as byte Array"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_download_file"
android:text="Download as File"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:contentDescription="@null" />
</LinearLayout>
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DownloadActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 558
Reputation: 11224
You cannot store image files in the Gallery as the Gallery app is no storage place but just an app that shows all image files on your device. To know which image files are on your device the Gallery app queries the MediaStore. The MediaStore will have indexed the image files on your device.
If you create a new file you should inform the MediaStore about it. Otherwise the Gallery app will not 'see' it.
Upvotes: 1