我希望我的newfeed.java标签允许用户从图库和>中选择图片。将其上传到firebase存储。另外,如何检索这些图像并在列表视图中显示?
Newfeed.java
import static android.app.Activity.RESULT_OK;
public class NewfeedTab extends Fragment {
private static final int PICK_IMAGE_REQUEST = 234;
private StorageReference mStorageRef;
private EditText story;
private ImageButton choose;
private ImageButton upload;
private Uri filePath;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news_feed, container, false);
mStorageRef = FirebaseStorage.getInstance().getReference();
story = (EditText)rootView.findViewById(R.id.et_story);
choose = (ImageButton)rootView.findViewById(R.id.imageButton_choose);
upload = (ImageButton)rootView.findViewById(R.id.imageButton_upload);
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(filePath != null){
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle("Uploading");
progressDialog.show();
StorageReference picsRef = mStorageRef.child("images/pic.jpg");
picsRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getContext(),"Story posted",Toast.LENGTH_SHORT);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getContext(),"Story couldn't be posted",Toast.LENGTH_SHORT);
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
}
});
}
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
}
}
}
从图库中选择图片后关闭应用程序。 NewfeedTab.java是标签活动的一个片段。
newsfeed.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="com.seven.pprs.bloodlink.TabActivity$PlaceholderFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/et_story"
android:layout_toStartOf="@+id/imageButton_choose"
android:hint="Share your stories here" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_file_upload_black_24dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="@+id/imageButton_upload" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:dividerHeight="1dp"
android:divider="@android:color/holo_red_dark"
android:id="@+id/list_of_posts"
android:layout_marginBottom="16dp"
android:layout_below="@+id/et_story" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_add_a_photo_black_24dp"
android:layout_alignBottom="@+id/imageButton_upload"
android:layout_toStartOf="@+id/imageButton_upload"
android:id="@+id/imageButton_choose" />
答案 0 :(得分:0)
您是否拥有AndroidManifest.xml中的读取权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>