片段将图像上传到ImageButton

时间:2017-09-27 18:40:51

标签: android android-fragments

我尝试了很多不同的方法,但似乎都没有帮助,所以问题如下: 图片不会上传到ima​​geButton。我有路径,我通过调试器检查它,添加了一些Toasts到片段到达onActivityResult,它确实,但图像不上传。有什么想法吗?

public class AddProductFragment extends Fragment {
    private static final String TAG = "AddProductFragment";
    private static final int GALLERY_INTENT = 2;
    private Uri imageUri;
    private ImageButton imageButton;
    private EditText productName,produtPrice,productDescription;
    private Button btnCancel,btnAdd;
    private FirebaseDatabase mDb;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDb = FirebaseDatabase.getInstance();
    }

    private void onClickEvents() {
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(Intent.createChooser(i,"Select Picture"),GALLERY_INTENT);
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO: Edit this;
                String name = productName.getText().toString();
                String price = produtPrice.getText().toString();
                String description = productDescription.getText().toString();
                DatabaseOperation dbOps = new DatabaseOperation(mDb,getContext());
                dbOps.AddProduct(imageUri,name,price,description);
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO: exit fragment
            }
        });
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.fragment_add_product, container, false);
        imageButton = (ImageButton) v.findViewById(R.id.imageButton2);
        productName = (EditText)v.findViewById(R.id.addProduct_productName);
        produtPrice = (EditText)v.findViewById(R.id.addProduct_productPrice);
        productDescription = (EditText)v.findViewById(R.id.addProduct_productDescription);
        btnAdd = (Button) v.findViewById(R.id.addProduct_addBtn);
        btnCancel = (Button) v.findViewById(R.id.addProduct_cancelBtn);
        onClickEvents();
        return v;
    }

    public Bitmap getBitmap(String path) {
        Bitmap bitmap = null;
        try {
            File f = new File(path);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
            imageButton.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(AddProductFragment.this.getActivity(), "Number 1", Toast.LENGTH_SHORT).show();
        if(requestCode == GALLERY_INTENT){
            Toast.makeText(AddProductFragment.this.getActivity(), "Number 2", Toast.LENGTH_SHORT).show();
            if(resultCode == Activity.RESULT_OK) {
                Toast.makeText(AddProductFragment.this.getActivity(), "Number 3", Toast.LENGTH_SHORT).show();
                String path = getPathFromCameraData(data, this.getActivity());
                Toast.makeText(AddProductFragment.this.getActivity(), "Uploading", Toast.LENGTH_SHORT).show();
                if (path != null) {
                    setFullImageFromFilePath( path);
                    Toast.makeText(AddProductFragment.this.getActivity(), "Uploaded", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    public void setFullImageFromFilePath( String imgPath) {
//        btn.setImageBitmap(BitmapFactory.decodeFile(imgPath));
        getBitmap(imgPath);
    }

    public static String getPathFromCameraData(Intent data, Context ctx) {
        Uri selectedImage = data.getData();
        String[] pathToFile = {MediaStore.Images.Media.DATA};
        Cursor cursor = ctx.getContentResolver().query(selectedImage, pathToFile, null,
                null, null);
        if(cursor == null) return null;
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(pathToFile[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
            return picturePath;
        }

}

1 个答案:

答案 0 :(得分:0)

let str = "abc"
let nsRange = NSRange(location: 2, length: 1)
let sRange = Range(nsRange, in: str)