我正在尝试创建一个应用程序,您可以在按钮单击时从库中选择图像,选择图像后,应将其保存在firebase上,然后在我的活动中检索回来。问题是我已经成功保存了我的firebase数据库中的图像,但是当我的应用程序启动时,它没有在我的ImageView中显示图像。
以下是我的java代码。我做错了请帮帮我
public class MainActivity extends AppCompatActivity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
Firebase ref;
ImageView imgView;
String encodedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ref = new Firebase("https://setfireimage.firebaseio.com");
imgView = (ImageView) findViewById(R.id.imgView);
byte[] dec = Base64.decode(encodedImage,Base64.DEFAULT);
Bitmap decodeByte = BitmapFactory.decodeByteArray(dec,0,dec.length);
imgView.setImageBitmap(decodeByte);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
Bitmap bm = BitmapFactory.decodeFile(imgDecodableString);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] byteArray = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArray,Base64.DEFAULT);
ref.push().setValue(encodedImage, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if(firebaseError != null){
Toast.makeText(getApplicationContext(),firebaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Image Saved",Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="random.setimagefirebase.MainActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageView>
<Button
android:id="@+id/buttonLoadPicture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="loadImagefromGallery"
android:text="Load Picture" >
</Button>
</LinearLayout>
答案 0 :(得分:0)
基本上,你想要做的是这样的事情:
在Activity
中创建一个常量,该常量将对应此图像的子名称:
private final String IMAGE = "image";
然后,在你的onCreate
中你需要做这样的事情:
ref.child(IMAGE).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.getValue(String.class);
if(data != null) {
byte[] dec = Base64.decode(data,Base64.DEFAULT);
Bitmap decodeByte = BitmapFactory.decodeByteArray(dec,0,dec.length);
imgView.setImageBitmap(decodeByte);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
当您在FireBase
中创建此值时,您需要这样做:
mFirebaseRef.child(IMAGE).setValue(encodedImage, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if(firebaseError != null){
Toast.makeText(getApplicationContext(),firebaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Image Saved",Toast.LENGTH_SHORT).show();
}
}
});
而不是
ref.push().setValue(encodedImage, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if(firebaseError != null){
Toast.makeText(getApplicationContext(),firebaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Image Saved",Toast.LENGTH_SHORT).show();
}
}
});
因此,当您更改它时,它会自动调用onDataChanged
onCreate