我正在尝试查看图库中的图片,这是我的应用目前的样子
这是我的 MainActivity.java
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public static Context contextOfApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contextOfApplication = getApplicationContext();
setContentView(R.layout.activity_home);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Override this method in the activity that hosts the Fragment and call super
// in order to receive the result inside onActivityResult from the fragment.
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
fragment.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
public static Context getContextOfApplication()
{
return contextOfApplication;
}
}
这是我的片段代码
imageupload.java
public class UploadDisplayPicFragment extends Fragment {
Button btnChooseImage, btnUploadImage;
ImageView imageUpload;
final int CODE_GALLERY_REQUEST = 999;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View fragment_view = inflater.inflate(R.layout.fragment_upload_display_pic, container, false);
btnChooseImage = (Button)fragment_view.findViewById(R.id.btnChooseImage);
btnUploadImage = (Button)fragment_view.findViewById(R.id.btnUploadImage);
imageUpload = (ImageView) fragment_view.findViewById(R.id.imageView);
btnChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//set permission to access gallary
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE},
CODE_GALLERY_REQUEST
);
}
});
return fragment_view;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if(requestCode == CODE_GALLERY_REQUEST)
{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Intent gallaryIntent = new Intent(Intent.ACTION_PICK);
gallaryIntent.setType("image/*");
startActivityForResult(gallaryIntent.createChooser(gallaryIntent,"Select Image"),CODE_GALLERY_REQUEST);
}else{
Toast.makeText(getActivity(), "You do NOT have Permission", Toast.LENGTH_SHORT).show();
}
return ;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CODE_GALLERY_REQUEST && requestCode == RESULT_OK && data != null)
{
Uri filepath = data.getData();
Context applicationContext = HomeActivity.getContextOfApplication();
try {
InputStream inputStream = applicationContext.getContentResolver().openInputStream(filepath);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageUpload.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
该应用程序不会崩溃或发生任何事情,但是当我选择图像时,它不会显示在框中
以防万一,您需要额外的注释:我正在关注本教程,但不同之处在于他们在活动中使用它,而我正在使用片段 https://www.youtube.com/watch?v=ULHyRwep3EU&t=1s